C library functions -ferror()
C Standard Library - <stdio.h>
Description
C Library Functionsint ferror(FILE *stream)Tests the error indicator for the given stream.
Declaration
Below is the declaration of the ferror() function.
int ferror(FILE *stream)
Parameter
- stream-- This is a pointer to a FILE object that identifies the stream.
Return Value
If the error indicator associated with the stream is set, the function returns a non-zero value; otherwise, it returns a zero value.
Example
The following example demonstrates the usage of the ferror() function.
#include <stdio.h>
int main()
{
FILE *fp;
char c;
fp = fopen("file.txt", "w");
c = fgetc(fp);
if( ferror(fp) )
{
printf("读取文件:file.txt 时发生错误\n");
}
clearerr(fp);
if( ferror(fp) )
{
printf("读取文件:file.txt 时发生错误\n");
}
fclose(fp);
return(0);
}
Suppose we have a text filefile.txt, which is an empty file. Let us compile and run the above program, because we attempt to read a file opened in write-only mode, this will produce the following result.
读取文件:file.txt 时发生错误other extensions