File end function (feof)

Source: Internet
Author: User

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;
}

 

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.