I am using fgets()
to scan a file as follows:
char buf[50];
if (fgets(buf, 50, fp) == NULL) {
printf("Error in file parsing.\n");
exit(1);
}
char *p;
p = buf;
p
points to buffer and I am using it to iterate through the scanned string. fgets()
has size 50, but it does indeed add a null terminator at the end of the scanned string.
My while loop looked as follows:
while (*p != '\0')
This worked when my text file had: 1 + 23
. When the text file contained 1 + 23 - 5
, it hits an infinite loop. Why is this happening?
I also tried checking for \n
which also failed. At the end, I used strlen
and had a for loop run according to strlen
but that wasn't accurate.
Any suggestions on what my while loop should look like?