GCC: fclose() Error

Nava Whiteford new@sgenomics.org
Thu Dec 3 00:20:00 GMT 2009


>> As you will notice in the sequence of steps in the illustrative code above, in line 6 a fgetc() call to file 'f' is made instead of 'f1'. This situation, a cause of segmentation fault on a non-Cygwin machine, was not encountered by Cygin's gcc compiler. In fact, the code compiles and runs successfully on Cygwin.
>>
>> Would appreciate assistance with addressing the situation.
>
> The best approach is to fix your program so that it doesn't use 'f'  
> after it has been closed.  It is not really related to gcc, but just  
> good practice in general.

I would agree. 

In this instance however I might guess that windows is reusing file handle IDs whereas others machines are not?



Your code as a complete program...

#include "stdio.h"

int main(int argc, char **argv) {
  char c;
  FILE * f;
  f = fopen(argv[1], "r");
  fclose(f);
  FILE * f1;
  f1 = fopen(argv[1], "r");
  while ((c = fgetc(f)) != EOF){
  }
}

Nav



More information about the Gcc-help mailing list