Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C library function - clearerr()

C library function - clearerr()

C Standard Library - <stdio.h> C Standard Library -

Description

C library function *void clearerr(FILE stream) Clears the end-of-file and error indicators of the given stream.

Declaration

The following is the declaration of the clearerr() function.



void clearerr(FILE *stream)

Parameters

Return Value

This never fails and does not set the external variable errno, but if it detects that its argument is not a valid stream, it returns -1 and sets errno to EBADF.

Example

The following example demonstrates the use of the clearerr() function.

Example

#include

int

main

(

)

{

   FILE

fp

;

   

char

c

;

   fp

=

fopen

(

"file.txt"

,

"w"

)

;

   c

=

fgetc

(

fp

)

;

   

if

(

ferror

(

fp

)

)

   

{

     

printf

(

"An error occurred while reading the file: file.txt\n"

)

;

   

}

   

clearerr

(

fp

)

;

   

if

(

ferror

(

fp

)

)

   

{

     

printf

(

"An error occurred while reading the file: file.txt\n"

)

;

   

}

   

fclose

(

fp

)

;

   

return

(

0

)

;

}

Suppose we have a text file file.txt, which is an empty file. Let us compile and run the program above; since we try to read a file opened in write-only mode, this will produce the following result.



读取文件:file.txt 时发生错误

C Standard Library - <stdio.h> C Standard Library -