- #1
- 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:
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?
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?