Function feof (fp // file pointer) is used to test whether the internal position pointer of the file pointed to by fp points to the end of the file. If the file ends, the value of the feof (fp) function is 1 (true). If it does not end, the value of the feof (fp) function is 0 (false ), the feof function can determine the end of binary and text files.
Example:
Copy the data of a local file to another file (a.txt is an existing file)
[Cpp]
# Include <stdio. h>
Int main ()
{
FILE * fpin, * fpout;
Char ch;
If (fpin = fopen ("d: \ a.txt", "rt") = NULL) // open the source file
{
Printf ("Cannot open a.txt \ n ");
}
If (fpout = fopen ("d: \ B .txt", "wt") = NULL) // open the target file
{
Printf ("cannot open B .txt \ n ");
}
Ch = fgetc (fpin); // read the first character of the original file
Printf ("directly output the content of file a \ n ");
While (! Feof (fpin) // determines whether the source file ends.
{
Putchar (ch );
Fputc (ch, fpout );
Ch = fgetc (fpin );
}
Printf ("\ n ");
Fclose (fpin );
Fclose (fpout );
Printf ("\ n Copy file a to file B and output file B content \ n ");
If (fpout = fopen ("d: \ B .txt", "rt") = NULL)
{
Printf ("Cannot open B .txt \ n ");
}
Ch = fgetc (fpout );
While (! Feof (fpout ))
{
Putchar (ch );
Ch = fgetc (fpout );
}
Fclose (fpout );
Printf ("\ n ");
Return 0;
}
# Include <stdio. h>
Int main ()
{
FILE * fpin, * fpout;
Char ch;
If (fpin = fopen ("d: \ a.txt", "rt") = NULL) // open the source file
{
Printf ("Cannot open a.txt \ n ");
}
If (fpout = fopen ("d: \ B .txt", "wt") = NULL) // open the target file
{
Printf ("cannot open B .txt \ n ");
}
Ch = fgetc (fpin); // read the first character of the original file
Printf ("directly output the content of file a \ n ");
While (! Feof (fpin) // determines whether the source file ends.
{
Putchar (ch );
Fputc (ch, fpout );
Ch = fgetc (fpin );
}
Printf ("\ n ");
Fclose (fpin );
Fclose (fpout );
Printf ("\ n Copy file a to file B and output file B content \ n ");
If (fpout = fopen ("d: \ B .txt", "rt") = NULL)
{
Printf ("Cannot open B .txt \ n ");
}
Ch = fgetc (fpout );
While (! Feof (fpout ))
{
Putchar (ch );
Ch = fgetc (fpout );
}
Fclose (fpout );
Printf ("\ n ");
Return 0;
}