首页 > 解决方案 > 如何在 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.

标签: cfilegetcferror

解决方案


推荐阅读