Reputation: 175
Ok, so I have a program that reads lines of a file. However once it reaches the line
*** END ***
it's meant to see that
char str[100];
while(fgets(str,100,stdin) != NULL && strcmp(str,"*** END ***"))
and the while loop should stop because strcmp will be equal to 0 therefore making the while loop false.
However it doesn't. I think this is because str
has a different amount of chars (I'm assuming the rest after the copied line have nothing inside of them?) than "*** END ***"
. How can I fix my program so that it will end once the line is read? Thanks.
OK, so I know now it's because in my example text, I had a line after that one. Once deleted it works fine. But how can I make it end whether there's a line after it or not?
Upvotes: 0
Views: 313
Reputation:
you can use
char * strstr ( const char *, const char * )
A pointer to the first occurrence in str1 of the entire sequence of characters specified in str2, or a null pointer if the sequence is not present in str1.
Upvotes: 1
Reputation: 2328
I think your input terminates with an end of line (\n
) character. fgets
include that also in the string.
Try strcmp(str,"*** END ***\n")
instead.
Upvotes: 0