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.

KWJ2104
  • 1,959
  • 6
  • 38
  • 53
  • As for dealing with the problem, there's http://stackoverflow.com/questions/2693776/removing-trailing-newline-character-from-fgets-input – John Flatness Oct 30 '11 at 03:05

2 Answers2

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.

sashang
  • 11,704
  • 6
  • 44
  • 58
  • Remove the probably and you get +1 :) – FailedDev Oct 30 '11 at 03:03
  • @sashang 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? – Andy Dec 03 '12 at 11:06
  • @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. – sashang Dec 03 '12 at 22:50
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.

AusCBloke
  • 18,014
  • 6
  • 40
  • 44
  • am I to assume that when `fgets` reads, say, `"Hello"`, it actually reads `"Hello\n\0"` or `"Hello\n"`. – Andy Dec 03 '12 at 11:08
  • 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. – AusCBloke Dec 03 '12 at 12:07
  • 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. – Andy Dec 03 '12 at 22:33
  • `strlen` counts *every* character *up until* a `'\0'`. So, yes, it includes the `'\n'`. – AusCBloke Dec 03 '12 at 22:46
  • 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) – J-a-n-u-s May 29 '16 at 23:24