strstr not functioning
Asked Answered
L

2

5

Why does this particular piece of code return false on the strstr() if I input "test"?

char input[100];

int main()
{
    fgets(input, 100, stdin);
    printf("%s", input);

    if(strstr("test message", input))
    {
        printf("strstr true");

    }


}

I thought strstr searched the first param for instances of the second param? It works when I replace input with some text or just assign it something directly, but it seems to not work with fgets.

Lectra answered 30/10, 2011 at 2:56 Comment(1)
As for dealing with the problem, there's #2694276Art
B
11

It's because fgets stores the newline character so when strstr does a comparison it fails.

From the man page:

fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A '\0' is stored after the last character in the buffer.

Bruiser answered 30/10, 2011 at 3:2 Comment(3)
Remove the probably and you get +1 :)Sibyl
@Bruiser that document part is still confusing. It says EOF or newline. Which is it? I was going through a tutorial and it was failing me. Replacing the last character with a null \0 did the trick but the tutorial made it seem like fgets automatically puts \0 when in fact it puts \n. Because of this strstr was not functioning. But then why did the book say it would work?Tubuliflorous
@Andy: 'EOF or newline' means either EOF or newline or both of them. 'or' here has the meaning of the logical boolean or and not 'or' that is used in colloquial English. Therefore both of them is a valid answer.Bruiser
O
7

Add input[strlen(input) - 1] = '\0'; after the fgets. fgets reads in the newline char ('\n'). There is no '\n' in "test message" so input will never be contained within it.

You should really check to see if the newline is at the end of the buffer after calling fgets to know if the whole line was able to actually fit into it, and also to obviously remove it.

Observable answered 30/10, 2011 at 3:3 Comment(5)
am I to assume that when fgets reads, say, "Hello", it actually reads "Hello\n\0" or "Hello\n".Tubuliflorous
It reads "Hello\n" and then terminates the string with a '\0'. But that's only if there's a newline character to be read.Observable
Let me rephrase the question. What should the total result be of fget? I ask because strstr isn't working. When i do variable[strlen(variable)-1] = '\0', it works. When strlen parses the string for the length, does it skip the terminating null value? So "Hello\n\0", is it 6 because it includes the newline? And if so, then the result would be "Hello\0\0" if thats how I would solve my problem which is a bit wasteful. Thanks for your time by the way.Tubuliflorous
strlen counts every character up until a '\0'. So, yes, it includes the '\n'.Observable
Addendum: If fgets() is reading from a text file, it will read "Hello\n" if your encoding uses LF for new line but if your encoding uses CRLF for new line it will read "Hello\r\n" N.B. LF means Line Feed (cursor drops to next line but remains in same column) and CR means Carriage Return (cursor moves to beginning of line)Amon

© 2022 - 2024 — McMap. All rights reserved.