Trouble with fgets in C: Find My Stupid Mistake

  • Thread starter CRGreathouse
  • Start date
In summary, the person is having trouble with reading past the end-of-file in C and is asking for help in finding their mistake. They provide a code snippet that causes the error and explain that the last read through fgets does not return NULL but instead causes an application-ending error. They mention that they fixed the issue with a rewrite and speculate that the problem may have been caused by the surrounding code.
  • #1
CRGreathouse
Science Advisor
Homework Helper
2,844
0
I've been having trouble with reading past the end-of-file in C. Can anyone find my stupid mistake?

This is the minimal code needed to cause the error for me:

Code:
FILE *f = fopen(name, "r");
if (!f)
	return;
pari_sp ltop = avma;
char line[1100];
while(fgets(line, 1100, f) != NULL)
	printf(".");
fclose(f);

So name is a C-string pointing to a valid filename. The FILE f opens without trouble, and the fgets loop runs through each line in the file. (The actual processing code, which I deleted, parses those lines without trouble; here I replaced it with a line which shows how many lines it reads.) But the last read through fgets does *not* return NULL but causes an application-ending error. fclose is never run.

What am I doing wrong?
 
Technology news on Phys.org
  • #2
Never mind, I fixed it with a rewrite. The problem must have been caused, by some mechanism I don't understand, by the surrounding (complicated) code.
 

Related to Trouble with fgets in C: Find My Stupid Mistake

1. What is the purpose of fgets in C?

Fgets is a function in the C programming language used to read a line of text from a specified stream. It is commonly used to read input from a user or from a file.

2. What is the most common mistake when using fgets in C?

The most common mistake when using fgets in C is forgetting to include the newline character (\n) at the end of the input. This can cause issues when comparing or manipulating the input later on.

3. How can I fix my fgets code if it is not working properly?

If your fgets code is not working properly, there are a few things you can try. First, make sure you have included the correct syntax and parameters for the function. You may also need to check for any errors using the perror() function. Additionally, double check that your input file or user input is formatted correctly.

4. Can I use fgets to read multiple lines of input?

Yes, fgets can be used to read multiple lines of input by using a loop and continuously calling the function until all desired lines have been read. Just be sure to include the newline character in the input so that each line is properly separated.

5. Are there any alternatives to using fgets in C?

Yes, there are other functions in C that can be used to read input, such as scanf() or gets(). However, fgets is often preferred for its ability to handle longer input and provide error checking. It is always important to carefully choose the appropriate function for your specific needs.

Similar threads

  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
4
Views
767
  • Programming and Computer Science
Replies
2
Views
406
  • Programming and Computer Science
Replies
4
Views
760
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
11
Views
34K
  • Programming and Computer Science
Replies
32
Views
2K
  • Programming and Computer Science
2
Replies
65
Views
5K
Back
Top