c - 如何在 ferror 文件中发出错误信号
问题描述
假设我有这个代码:
FILE *myfile=fopen("myfile.txt", "r");
if (!myfile) {...}
while (1) {
int c = getc(myfile);
if (ferror (myfile)) {
perror ("get c error");
exit (EXIT_FAILURE);
} else if (c == EOF){
printf ("%s\n" , "the end");
break;
}
}
有没有办法手动导致文件出错,这ferror
是真的,而不会将模式更改fopen
为“w”?(这会导致错误,因为我们myfile
以写入模式打开,但我们使用getc
.
解决方案
推荐阅读
- r - Anova.III.lm 中的错误(mod,error,singular.ok=singular.ok,...):模型中存在别名系数
- python - 运行 Tkinter 后注销
- reactjs - 使用 react native 绘制形状的最简单方法是什么?
- c++ - 为什么不读取 2 个字符串?
- reactjs - 每次打开网页时如何添加React-bootstrap Modal
- c# - 只有外键的 EF Core 表
- c++ - c++ 十进制到二进制转换器的奇怪错误
- r - 用 R 抓取 youtube 视频,有可能吗?
- python - CSV to JSON python - 创建的 JSON 文件中的错误
- ruby-on-rails - 如何将 Base64 图像附加到 Active Storage 对象?