c语言ferror

C中的ferror()函数 (ferror() function in C)

Prototype:

原型:

    int ferror(FILE *filename);

Parameters:

参数:

    FILE *filename

Return type: int(0 or 1)

返回类型: int(0或1)

Use of function:

使用功能:

If in the last file operation there is some error introduced in that file, by the ferror() function we identify there is some error in that file during the last operation. The prototype of ferror() function is int ferror(FILE* filename);

如果在上一个文件操作中该文件中引入了一些错误,则通过ferror()函数,我们可以确定在上一个操作期间该文件中存在一些错误。 ferror()函数的原型是int ferror(FILE * filename);

But only detects the last operational error of the file because every time the error flag of the file is set.

但是只检测文件的最后操作错误,因为每次都设置文件的错误标志。

C中的ferror()示例 (ferror() example in C)

#include <stdio.h>
#include <stdlib.h>

int main()
{
	FILE *f;
	char str[100];

	//Check the existence of that file
	if((f=fopen("includehelp.txt","r"))==NULL){
		printf("Cannot open the file...");
		//if not exist program is terminated
		exit(1);
	}

	// Check if here is some error in the file
	if(ferror(f))
		printf("Error to read the file\n");
	else	
		printf("No error in reading\n");

	printf("File content is--\n");
	//print the strings until EOF is encountered
	while(!feof(f)){
		fgets(str,100,f);
		//print the string
		printf("%s",str);
	}

	//close the opened file
	fclose(f);

	return 0;
}

Output

输出量

翻译自: https://www.includehelp/c-programs/ferror-function-in-c-language-with-example.aspx

c语言ferror

更多推荐

c语言ferror_使用示例的C语言中的ferror()函数