• C

Problem with fgets

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)
    input(line);

}

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)
    count_addresses(temp);
  return;
}

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.
Kevin_P23Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kent OlsenData Warehouse Architect / DBACommented:
Howdy...

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.


Kent
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Kent OlsenData Warehouse Architect / DBACommented:

Oh -- and make sure that you reflect the new buffer size in the call to fgets()!
0
Kent OlsenData Warehouse Architect / DBACommented:

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,
Kent
0
Kevin_P23Author Commented:
Yep, works like a charm now. I knew there was something small I was overlooking. Thanks for your help.

Kevin
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.