f = fopen("filename", "r");
c = fgetc(f);
if(c < 0) {
fclose(f);
system("type filename > filename.tmp");
f = fopen("filename.tmp", "r");
}
else {
rewind(f);
}
while(fgets(buff, 256, f) != NULL)
puts(f);
fclose(f);
return(0);
}
Name:
Anonymous2012-01-12 22:31
>>17 C99 7.19.5.3.8 The fopen function returns a pointer to the object controlling the stream. If the open operation fails, fopen returns a null pointer.
So what happens if fopen() returns a NULL pointer and you call fgetc() on it? C99 7.1.4.1 Each of the following statements applies unless explicitly stated otherwise in the detailed descriptions that follow: If an argument to a function has an invalid value (such as a value outside the domain of the function, or a pointer outside the address space of the program, or a null pointer, or a pointer to non-modifiable storage when the corresponding parameter is not const-qualified) or a type (after promotion) not expected by a function with variable number of arguments, the behavior is undefined.
The Standard provides no exceptions for fgetc(), so it's undefined behavior.