Determine the end function feof and end function feof

Source: Internet
Author: User

Determine the end function feof and end function feof

In the previous example 2, the program reads characters one by one from a file and outputs them to the screen for display. In the while LOOP, EOF is used as the end mark of the file. This type of file with EOF as the end mark must be a text file. In text files, data is stored in the form of ASCII values of characters. We know that the ASCII value ranges from 0 ~ 255.-1 is not possible, so EOF can be used as the end mark of the file.

When the data is stored in a binary file, the value-1 is displayed. In this case, the EOF cannot be used as the end mark of the binary file. To solve this problem, ansi c provides a feof function to determine whether the file ends. If the file ends, the value of the feof (fp) function is 1; otherwise, the value is 0. The feof function can be used to determine whether a binary file ends or whether a text file ends.

Example 1: write a program to copy a text file (source file) to another file (target file. The source file name is file_a.dat, and the target file name is file_ B .dat.

The procedure is as follows:

1 # include <stdio. h> 2 # include <stdlib. h> 3 4 // declare the FILE copy function 5 void filecopy (FILE * fpin, FILE * fpout); 6 7 int main () 8 {9 FILE * fpin, * fpout; 10 11 fpin = fopen ("D :\\ TEST \ file_a.dat", "r"); // open the input file 12 if (fpin = NULL) 13 {14 printf ("Can't open this file! \ N "); 15 exit (0); // use the exit function, which must contain stdlib. h header file 16} 17 18 fpout = fopen ("D: \ TEST \ file_ B .dat", "w"); // open the output file 19 20 filecopy (fpin, fpout ); // File Replication 21 22 // close the input and output files 23 fclose (fpin); 24 fclose (fpout); 25 return 0; 26} 27 28 // defines the filecopy function 29 void filecopy (FILE * fpin, FILE * fpout) 30 {31 char ch; 32 ch = getc (fpin ); // read a character first and use the feof function to determine whether the file is empty. 33 // when the end mark of the file is encountered, the feof (fp) value of the function is 1; otherwise, it is 034 while (! Feof (fpin) 35 {36 putc (ch, fpout); 37 ch = getc (fpin); // copy characters one by one 38} 39}View Code

Source file:

  

Target file:

  

Related Article

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.