Search code examples
clinuxfile-ioeoffgets

fgets is reading more data from a closed filedescriptor


I am facing some issues with fgets() on linux. fgets is returning me data from a closed filedescriptor. I am expecting it to return EOF if the file got closed. I am able to come up with a simplified version of my problem.

int main()
{
        int i=0, j;
        FILE *FD;
        char p[128];
        FD = fopen("junk", "r");
        while(fgets(p, sizeof(p), FD))
        {  
                close(fileno(FD));
                printf("lineno=%d\n", i++);
        }  
        return 0;
}

I expect it to print just one line. Is there any workaround for this problem ?

Update: As someone answered below, fgets is buffering the file and returning me data even after the FD is closed. It is buffering 4kB of data. If the file size is more than 4k, it gets EOF after reading 4KB and the printing stops. Else if the file size is less thank 4k, it prints till the end of the file.


Solution

  • Like others have said, close(fileno(FD)); is a bad idea. If you do that, the same file descriptor might be reused for another file and fgets will suddenly read that fd instead. This is especially likely for multithreaded applications.

    Also, fgets is buffered. It has probably read a large chunk of data or so from the file the first time it's called, and following calls just returns more data from the buffer.

    Instead, do something like this:

    int main(void)
    {
            int i=0, j;
            FILE *FD;
            char p[128];
            FD = fopen("junk", "r");
            while(FD && fgets(p, sizeof(p), FD))
            {  
                    fclose(FD);
                    FD = NULL;
                    printf("lineno=%d\n", i++);
            }  
            return 0;
    }