开发者

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.


Calling fgets() on a closed file has undefined behavior. In other words, it is not guaranteed to return EOF.


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;
}


Using a file descriptor after closing it is likely going to result in unpredictable/undefined behavior. You probably should avoid doing that.

What is probably happening in the posted example is that the stream is opened and read. A read of the data will possibly put more data into the underlying stream. So even though the underlying file descriptor is closed, the handle to the stream would still contain more data to read. Again, this is probably unpredictable.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜