The C language file input and output error detection is just a copy of Tan haoqiang's book for future reference.

Source: Internet
Author: User

1. Determine whether the file is successfully opened:

Whether the file is successfully opened or not. if (fp = fopen ("filename", "r") = NULL), the file fails to be opened.

 

2. Determine whether the file is ended:

To determine whether the ASCII file ends, fgetc () will return the end mark EOF (-1). Because the binary file data will show-1, you must use the feof (fp) function to determine whether the end mark is "-1", and feof (fp) = 1. So you can read a file while (! Feof (fp) {c = fgetc (in );
...}


3. ferror ()
When calling a variety of input and output functions, if there is a dislocation, in addition to reflecting the function return value, you can also use the ferror function to check. The call form is ferror (fp );
If the return value of ferror (fp) is 0, no error is returned. If an error occurs, a non-0 value is returned.
Each time an input/output function is called for a file, a new ferror function value is generated. Therefore, the return value of the ferror function should be checked immediately after an input/output function is called, otherwise, the information is lost.
When the fopen function is executed, the initial value of the ferror function is automatically set to 0;

 

4. clearerr ()
Clearerr (fp) sets the file error mark and end mark to 0. If an error occurs when an input/output function is called, The ferror function value is a non-0 value. After clearerr (fp) is called, the value of ferrer (fp) is changed to 0.
If an error mark is displayed, it is retained until the clearerr function or rewind function or any other input/output function is called for the same file.

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.