From the documentation of fgets
:
Reading stops when a newline character is found, at end-of-file or error. The newline, if any, is retained.
This means that fgets
will not remove the final '\n'
from the end of the read string. Thus, your strings are:
- "Dan Brown"
- "Dan Brown"
- "Dan Brown\n"
They're not equal.
This is a very common issue when using fgets
. That's why I usually prefer scanf
, like this:
char buffer[BUF_LEN];
char format[16];
int scanf_result;
sprintf(format, "%%%u[^\n]", BUF_LEN);
//....
do
{
//TODO: Ask for input
scanf_result = scanf(format, buffer);
switch (scanf_result)
{
case -1: //TODO: Print error message and exit
case 0: //TODO: Print error mesage and break
}
//Discard remainings of buffered input line
while (getchar() != '\n') {;}
} while (1); //Ugly, but plain
Otherwise, you can use fgets
with something like this:
int buf_len;
//TODO: Ask for input
while (fgets(buffer, BUF_LEN, stdin) == NULL)
{
//TODO: Check and handle error
}
buf_len = strlen(buffer);
//Remove trailing '\n', if present
if (buffer[buf_len - 1] == '\n')
{
buffer[--buf_len] = '\0';
}
Even though it's easier, I don't like this second method, because strlen
scans the string another time to determine its length. In most cases, this is not a performance issue, I avoid it because I have my own mental issues.