I guess it must be related with fgets and the fact that it incorporates EOF as another character at the end of string str, so when I compare it with "quit" it's not the same string.
Nope.
First of all, EOF
doesn't exist as a character, it's just a flag returned by some IO functions to tell you that the file finished while trying to read it.
What fgets
leaves in the string is the newline character (if it stopped because of it and not because of EOF or of an error). You can easily remove it - if it's present - after acquiring the string:
size_t l=strlen(str);
if(l && str[l-1]=='\n')
str[l-1]=0;
But most importantly, you are getting the comparison with "quit"
wrong.
If you write str != "quit"
you are performing a pointer comparison between str
and "quit"
, which will always fail since str
and "quit"
are distinct character arrays, and thus refer to different locations in memory. What you want is to compare the content of the two strings; in C, that is done with strcmp
:
while(strcmp(str, "quit")!=0)