# 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