How to signal an error in a file for ferror

57 views Asked by At

Suppose I have this code:

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;
   }
}

Is there a way to cause an error in the file manually , so ferror is true, without changing the mode in fopen to "w"? (This would cause an error because we opened myfile in write mode , but we read with getc.

0

There are 0 answers