Removing trailing newline character from fgets() input
Posted
by sfactor
on Stack Overflow
See other posts from Stack Overflow
or by sfactor
Published on 2010-04-22T19:21:18Z
Indexed on
2010/04/22
19:23 UTC
Read the original article
Hit count: 281
i am trying to get some data from the user and send it to another function in gcc. the code is something like this.
printf("Enter your Name: ");
if(!(fgets(Name, sizeof Name, stdin) != NULL)) {
fprintf(stderr, "Error reading Name.\n");
exit(1);
}
However, i find that it has an \n character in the end. so if i enter John it ends up sending John\n. so how do i remove that \n and send a proper string.
© Stack Overflow or respective owner