Problem with fgets

Posted on 2004-11-11
Last Modified: 2010-04-15
While reading a file using fgets in a while loop, fgets comes back to the first line. Any ideas?

first couple lines of infile:
 0   9464
 1   9565

/* call in main() */
  while (fgets(line, 10, infile) != NULL)


void input(char in[])
  char line_copy[10], *address, *value, code;
  int temp;

  strcpy(line_copy, in);
  address = strtok(line_copy, " ");
  value = strtok(NULL, " ");
  code = value[0];
  temp = ((atoi(value))%1000);

  if (code > 65)

DDD say that after the first call, line == " 0   9464" which is what I want.
The second iteration says line == "\n\000   9464", but shouldn't it be " 1   9565"?. This causes a seg. fault somewhere in input. I don't think this is too complicated but I need an answer fast!
Thanks in advance.
Question by:Kevin_P23
    LVL 45

    Accepted Solution


    Your input buffer is too small.  fgets() is transferring enough characters to fill up the buffer and stopping because it hasn't seen a new-line character.

    Increase the size of the input buffer to be larger than the longest expected line.

    LVL 45

    Expert Comment


    Oh -- and make sure that you reflect the new buffer size in the call to fgets()!
    LVL 45

    Expert Comment


    What you're seeing in the DDD is the result of two reads (calls to fgets()).  The first read grabs the first ten characters of the file and quits because the buffer is full.  That line ends with "9464".

    The second read immediately "sees" the new-line character, transfers it to the buffer and writes a line terminator.  The rest of the buffer remains unchanged and still contains the data from the first read.

    Looking at the data that you posted, you can see the new-line ('\n'), the line terminator ('\00'), and the rest of the data from the first read ("0   0464").

    "\n\000   9464"

    As I said, just make sure that the buffer is large enough and all of this just magically "works".

    Good Luck,

    Author Comment

    Yep, works like a charm now. I knew there was something small I was overlooking. Thanks for your help.


    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
    This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
    The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now