Kompetenzbolzen
Kompetenzbolzen

Reputation: 13

Funky behaviour of feof() under Windows

So I’ve been writing a program which scans through files that works just fine when compiled with gcc and clang on Linux. But on Windows, with both Microsoft compiler and MinGW, feof() false-triggers. The loop should break once it detects the end statement of the filetype, the feof is only there as a failsafe and never triggers for correct files on Linux…

I had the programm print the filepointer location with ftell() and the values are just plain wrong. The EOF always triggers at 2^n values which are some orders of magnitude lower than the actual file size...

while(1)
    {
            ...
            //File is read here
            //normally breaks before EOF-check

            if(feof(in))
            {
                    DEBUG_PRINTF("Reached EOF before IEND\n");
                    break;
            }
    }

EDIT:

opening the File with "rb" instaed of "r" solved the problem

Upvotes: 0

Views: 297

Answers (1)

Joshua
Joshua

Reputation: 43268

You opened your file with fopen(..., "r"). It's a text file. ftell() is going to return the correct values to pass back to fseek() which are not the same as the the values as the number of bytes you read because the library is handling \r\n -> \n for you.

In addition, you either have opened a file >= 4GB (msvcrt.dll can't handle files that big) or have a 0x1A byte in the file.

Please note that linking against msvcrt.dll is the default when using gcc on Windows, and it's about the equivalent to Visual Studio 6 runtime. You probably don't want to link against it. It's really buggy. Alas this question for how to not do so is unanswered.

Upvotes: 1

Related Questions