Even with the code you post, and the answers you have received about the use of fgetc(3)
, the code you have posted should not hang. Let's see what happens:
fgetc(3)
returns int
-1
value (as an int
, as normally is so defined the constant EOF
) to differentiate the 256 possibilities for an 8 bit char
, plus one more, for the EOF
condition (which is not a data value coming from the stream) and it is converted to a char
(making EOF
simply an alias of some valid input). In you case, you have assimilated that condition to one of the values of the char
type, by error.
But that doesn't normally makes the program to hang (it doesn't even define Undefined Behaviour, except for the fact that we actually don't know which char
value will be used as an alias of the EOF
condition). The program will have then two conditions for break
ing the loop (as you have masked one). The first being the real EOF
condition, and the second is receiving from the input stream actually the char
that EOF
is being converted to when you input it.
As somebody points in the comments. The problem must be in the large, commented block, you have deliverately hidden. Even if the printf(3)
you execute after it does, and the outer of the block doesn't, you can have errors in the hidden block, that manifest only when leaving it, and make the error apparent to be in the loop test you make at the beginning of the code block.
Changing the char in = '1';
to int in = '1';
is a good choice to eliminate the problem of a spureous break
s, but your hanging problem is not there... you have to post the hidden block to get more feedback.
IMPORTANT NOTE
Try to get used to write complete failing examples... Don't edit the code just to post it, without testing that it continues failing (as yours doesn't, I demonstrate that below), and you will not get this kind of answers (that seem more a flame than a try to help you). The code you posted, at least must be edited to make it runnable, as you have pasted only a snippet of code that, as such, is even not compilable. Some such example, should be like the one I post below, but, as it doesn't fail, cannot be used to fix your problem. Right?
#include <stdio.h> /* line added to make runnable */
#define filename "test.txt" /* line added to make runnable */
int main() /* line added to make runnable */
{ /* line added to make runnable */
char in = '1';
FILE *fid = fopen(filename, "r");
while (in != '0') {
printf("Start of loop, in is %c\r\n", in);
if ((in = fgetc(fid)) == EOF)
in = '0';
/* Large block that does stuff depending on the value of in*/
printf("Done outer loop, in is %c\r\n", in);
}
fclose(fid);
printf("Finished parsing events");
//fflush(stdout);
} /* line added to make runnable */
It runs as shown below (without any hung) (please, note that the sed(1)
command is used to add the four spaces needed to embed the output here as
output text, you must not use it at home for testing purposes):
$ pru8 | sed -e 's/^/ /'
Start of loop, in is 1
Done outer loop, in is p
Start of loop, in is p
Done outer loop, in is s
Start of loop, in is s
Done outer loop, in is d
[...] <--- a lot more of repeating lines like the ones posted above.
Start of loop, in is
Done outer loop, in is
Start of loop, in is
Done outer loop, in is 0
Finished parsing events
$ _
and no hung.