C Language File Operation function Ferror & feof & Clearerr

Source: Internet
Author: User
Tags rewind

These functions are related to errors in file reading and writing, and are analyzed below:

1:ferror

Prototype: int ferror (FILE * fp)

Function: Tests whether a file stream is set with an error identifier, or returns 0 if it is a non-0 integer.

Example:

 Shortch; FILE*f1;f1= fopen ("Test.txt","W"); Ch=fgetc (F1);//Note that we open the file as write-only, but try to read a character to the file, and the operation fails, and the file stream is set on the error identifierprintf"%d\n", ch);//here output-1, because when the FGETC function fails to return EOF, that is, -1if(ferror (F1)) {//test file stream for error identifiers with function Ferrorprintf"Have a error.\n"); //The result is naturally, so the output error message}fclose (F1);

Note: Each time a different operation error occurs, the last saved error identifier for the file stream is updated, for example: A and b two file operation errors have occurred, and a error identifier of 16,B error is 32, then the file stream save error identifier is 32, with Ferror (FP) The result of the read return is 32.

2:feof

Prototype: int feof (FILE * fp)

Function: Tests whether a file stream has a file end identifier set, or returns 0 if it is a non-0 integer.

Example:

FILE *F1; Shortch;f1= fopen ("Utf8.txt","R");/*The 16 binary structure of this file is: 31 22 (two bytes total)*/ while(1) {ch=getc (F1); //The third loop executes the GETC statement, since the file pointer already points to EOF, so this time the GETC function reads the EOF value (-1) and if the pointer is shifted back one bit, it does not point to any valid position (the pointer will not be offset). So then the file stream will be set on the file end identifier    if(feof (F1)) { Break; } printf ("%x\n", ch);} Fclose (F1);/*output: 3122Press any key to continue*/

Note: The Feof function detects the end of the file is very strange, it is not to detect whether the current file pointer to the EOF position, but to detect whether EOF has been read out, like the third cycle of the code above, then getc the EOF position read out, and then found that the pointer can no longer move back, Therefore, the file flow is set to the end of the identity of the file.

3:clearerr

Prototype:void clearerr(FILE * fp)

Function: Clears the file end of a file stream and the error identifier (that is, the file terminator and the error identifier are changed from a value other than 0 to a value of 0).

Example:

FILE *F1; Shortch;f1= fopen ("Utf8.txt","R"); while(1) {ch=getc (F1); if(feof (F1)) { Break; }}//the file stream has been set to the end of the file after the end of the loop flagprintf"%d\n", feof (f1) = =0);//output 0, indicating that feof returns a value other than 0, which detects that the file has reached the end positionClearerr (F1);//clear file stream end flag and error flagprintf"%d\n", feof (f1) = =0);//Output 1, indicating that feof returns a value of 0, which detects that the file has not reached the end position, although it has actually reached the end position.Fclose (F1);

Note: Call the file pointer to reset the function rewind (prototype:void Rewind(FILE * fp)), The end flag of the file stream is also cleared.

C language File manipulation functions Ferror & feof & Clearerr

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.