Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6531237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:51:12+00:00 2026-05-25T09:51:12+00:00

I am facing some issues with fgets() on linux. fgets is returning me data

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T09:51:13+00:00Added an answer on May 25, 2026 at 9:51 am

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm facing the need to enter ragged data in a WinForms application. Some columns
I am facing some issues while serializing objects (I am using JBoss Drools, and
Using WatiN, Im facing some issues with FireFox FF = new FireFox (http://www.google.com); ,
Well, since I am facing some issues with OAuth implementation, I decided to go
I have to create one expandable /collapseable panel in jQuery, and facing some issues.
I'm working using scriptaculous library. However I'm facing some issues with inclusion of the
I am new to ASP.Net MVC 3, facing some issues while trying to implementing
I'm using wamp in my system. I'm facing some issues in my project dicussed
Extremly confusing title, i know. Hello, i am facing some funky issues here. I
I am currenctly facing some htaccess/rewriterule issues. (And I am new to this area)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.