Question

I have a function that reads integers with certain format from a file. It works fine as desired, but whenever I tried to close the file with fclose(), fclose() always returns EOF.

Any suggestions why? I am a student and still learning. I have put the code below. Please let me know if you need the "processing" code. Thanks :)

// Open the file
FILE *myFile = fopen(fileName, "r");
if(myFile == NULL){
    //Handle error
    fprintf(stderr, "Error opening file for read \n");
    exit(1);
}

while(myFile != EOF)
{
     // read and process the file
     // this part works.
}

// always returns EOF here. WHY?
if (fclose(myFile) == EOF) {
    // Handle the error!
    fprintf(stderr, "Error closing input file.\n");
    exit(1);
}
printf("Done reading the file.");

EDIT: Thank you for all the replies. Sorry I cannot post the code as this is part of my homework. I was trying to get some help, I am not asking you guys to make the code for me. Posting code is illegal according to my Prof (since other students can see and probably copy, that's what he told me). I can only post the code after Sunday. For now, I will try to modify my code and avoid using fscanf. Thanks and my apology.

Was it helpful?

Solution

This:

while(myFile != EOF)

is actually illegal (a constraint violation). Any conforming C compiler is required to issue a diagnostic; gcc, by default, merely issues a warning (which does qualify as a "diagnostic").

If gcc gave you a warning, you should pay attention to it; gcc often issues warnings for things that IMHO should be treated as fatal errors. And if it didn't give you a warning, you're probably invoking it with options that disable warnings (which is odd, because it does produce that warning by default). A good set of options to use is -Wall -Wextra -std=c99 -pedantic (or adjust the -std=... option to enforce a different version of the standard if you like).

myFile is of pointer type, specifically FILE*. EOF is of type int, and typically expands to (-1). You cannot legally compare a pointer value to an integer value (except for the special case of a null pointer constant, but that doesn't apply here.)

Assuming the program isn't rejected, I'd expect that to result in an infinite loop, since myFile would almost certainly never be equal to EOF.

You could change it to

while (!feof(myFile))

but that can cause other problems. The correct way to detect end-of-file while reading from a file is to use the value returned by whatever function you're using read the data. Consult the documentation for the function you're using to see what it returns when it encounters end-of-file or an error condition. The feof() function is useful for determining, after you've finished reading, whether you encountered end-of-file or an error condition.

OTHER TIPS

Since there is nothing that you can do to a regular file open in read-only-mode that would cause a fclose to error out, you very probably have a bug in the code you didn't show which is stomping on the myFile structure.

Also the test myFile != EOF will never be true because you set it to the return of fopen which will never give you EOF and you already checked it for NULL Did you mean something like:

while((c = fgetc(myFile)) != EOF) {
    // do stuff
}

What the errno said? add this to your code:

#include <errno.h>
#include <string.h>

if (fclose(myFile) == EOF) {
    // Handle the error!
    fprintf(stderr, "Error closing input file. and errno = %d, and error = %s\n", errno, strerror(errno));
    exit(1);
}

Hope this help.

Regards.

while(myFile != EOF)
 {
 // read and process the file
 // this part works.
 }

If this part is ok then

fclose(myFile);

definitely return you EOF.Because the while loop terminates only myFile == EOF(this is a wrong comparison, do not ignore warnings).comparision between pointer and int.As EOF is a macro defined in stdio.h file And according to glibc it -1.Your loop terminates that means your myFile pointer changed to EOF some whire.This is your mistake.

just go through your code you must be change the FILE pointer myFile which should not be over written by you As it point to a file structure which is used for all file operation.

NOTE myFile which is a pointer to a file should not be used as lvalue in any assignment statement.

Change while(myFile != EOF){...} by while(!feof(myFile)){...}
myFile is a pointer to a FILE struct (a memory address). Not a "end of file" indicator.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top