Errors that are prone to mistakes in C language file operation streams

Source: Internet
Author: User
# Include <stdio. h> # include <stdlib. h> int main () {file * FP; FP = fopen ("E: \ recent files \ test.txt", "R"); // If W is used, then the followingCodeIf no write operation is performed, the source file will become empty. // your output will be useless. Therefore, pay attention to If (! FP) {printf ("can't find the files \ n. "); exit (1); // return to the operating system, close all open files system (" pause ");} Char ch; while (CH = fgetc (FP ))! = EOF) fputc (CH, stdout); // while (CH = fgetc (stdin ))! = EOF) // fputc (CH, stdout); fclose (FP); System ("pause"); Return 0 ;}

As we noted, we must remember not to make mistakes in this mode.

 

# Include <stdio. h> # include <iostream> using namespace STD; # include <stdlib. h> int main () {file * FP1, * fp2; char buffer [105]; FP1 = fopen ("E: \ recent files \ test.txt ", "W"); // fp2 = fopen ("E: \ recent files \ cherry.txt", "W "); cout <"111111fp1 =" <FP1 <Endl; If (null = FP1) {printf ("test cannt open \ n"); exit (1 );} char ch; while (CH = fgetc (stdin ))! = '\ N') {fputc (CH, FP1);} // fclose (FP1); cout <"222222fp1 =" <FP1 <Endl; system ("pause"); Return 0 ;}

Here we do not have fclose (); therefore, this file cannot be written successfully.

I do not know the specific reasons.Source codeRight

No fclose after fopen

The file will always be opened.ProgramExit. A file descriptor will be occupied until the program exits. If you keep fopen without fclose, the descriptor will leak.

 

# Include <stdio. h> # include <stdlib. h> int main () {file * FP1, * fp2; char buffer [105]; FP1 = fopen ("E: \ recent files \ test.txt ", "W"); fp2 = fopen ("E: \ recent files \ cherry.txt", "W"); If (null = FP1) {printf ("test cannt open \ n"); exit (1) ;}if (null = fp2) {printf ("Cherry cannt open \ n "); exit (1) ;}char ch; while (CH = fgetc (stdin ))! = '\ N') {fputc (CH, FP1);} fclose (FP1); FP1 = fopen ("E: \ recent files \ test.txt ", "R"); While (CH = fgetc (FP1 ))! = EOF) // while (! Feof (FP1) {fputc (CH, fp2) ;}fclose (fp2); fp2 = fopen ("E: \ recent files \ cherry.txt", "R "); while (CH = fgetc (fp2 ))! = EOF) {fputc (CH, stdout);} fclose (FP1); fclose (fp2); System ("pause"); Return 0 ;}

It is a pointer to close the file after each operation.

This is not clear yet

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.