ferror
From cppreference.net
File input/output
| Types and objects | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
定义于头文件
<stdio.h>
|
||
|
int
ferror
(
FILE
*
stream
)
;
|
||
检查给定流是否出现错误。
目录 |
参数
| stream | - | 要检查的文件流 |
返回值
若文件流发生错误则为非零值, 0 表示正常状态
示例
运行此代码
#include <stdio.h> #include <stdlib.h> #include <locale.h> #include <wchar.h> int main(void) { char* fname = tmpnam(NULL); FILE* f = fopen(fname, "wb"); fputs("\xff\xff\n", f); // 无效的UTF-8字符序列 fclose(f); setlocale(LC_ALL, "en_US.utf8"); f = fopen(fname, "rb"); wint_t ch; while ((ch=fgetwc(f)) != WEOF) // 尝试以UTF-8格式读取失败 printf("%#x ", ch); if (feof(f)) puts("EOF indicator set"); if (ferror(f)) puts("Error indicator set"); }
输出:
Error indicator set