Learn how to a build a cloud-first strategyRegister Now


Problem with fgets

Posted on 2004-11-11
Medium Priority
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
  • 3
LVL 46

Accepted Solution

Kent Olsen earned 2000 total points
ID: 12559808

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 46

Expert Comment

by:Kent Olsen
ID: 12559830

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

Expert Comment

by:Kent Olsen
ID: 12559925

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

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


Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them 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.
Suggested Courses
Course of the Month21 days, 2 hours left to enroll

810 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