Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

strcmp on a line read with fgets

I'm trying to compare two strings. One stored in a file, the other retrieved from the user (stdin).

Here is a sample program:

int main()
{
    char targetName[50];
    fgets(targetName,50,stdin);

    char aName[] = "bob";
    printf("%d",strcmp(aName,targetName));

    return 0;
}

In this program, strcmp returns a value of -1 when the input is "bob". Why is this? I thought they should be equal. How can I get it so that they are?

like image 474
Blackbinary Avatar asked Mar 08 '10 21:03

Blackbinary


People also ask

Does strcmp work with fgets?

And yes, now strcmp works.

Can you use fgets on Stdin?

Since fgets() reads input from user, we need to provide input during runtime. Reads characters from the standard input (stdin) and stores them as a C string into str until a newline character or the end-of-file is reached.

Does fgets read newline?

The fgets() function stores the result in string and adds a NULL character (\0) to the end of the string. The string includes the newline character, if read.

Can you use strcmp on chars?

The strcmp() compares two strings character by character. If the strings are equal, the function returns 0.


2 Answers

strcmp is one of the few functions that has the reverse results of true and false...if the strings are equal, the result is 0, not 1 as you would think....

if (strcmp(a, b)) {
    /* Do something here as the strings are not equal */
} else {
    /* Strings are equal */
}

Speaking of fgets, there is a likelihood that there is a newline attached to the end of the string...you need to get rid of it...

+-+-+-+--+--+
|b|o|b|\n|\0|
+-+-+-+--+--+

To get rid of the newline do this. CAVEATS: Do not use "strlen(aName) - 1", because a line returned by fgets may start with the NUL character - thus the index into the buffer becomes -1:

aName[strcspn(aName, "\n")] = '\0';

+-+-+-+--+
|b|o|b|\0|
+-+-+-+--+

Now, strcmp should return 0...

like image 145
t0mm13b Avatar answered Oct 30 '22 05:10

t0mm13b


fgets reads until it sees a newline then returns, so when you type bob, in the console, targetName contains "bob\n" which doesn't match "bob". From the fgets documenation: (bolding added)

Reads characters from stream and stores them as a C string into str until (num-1) characters have been read or either a newline or a the End-of-File is reached, whichever comes first. A newline character makes fgets stop reading, but it is considered a valid character and therefore it is included in the string copied to str. A null character is automatically appended in str after the characters read to signal the end of the C string.

You need to remove the newline from the end of targetName before you compare.

int cch = strlen(targetName);
if (cch > 1 && targetName[cch-1] == '\n')
   targetName[cch-1] = '\0';

or add the newline to your test string.

char targetName[50];
fgets(targetName,50,stdin);

char aName[] = "bob\n";
printf("%d",strcmp(aName,targetName));
like image 42
John Knoeller Avatar answered Oct 30 '22 07:10

John Knoeller