I'm a bit surprised that no one has mentioned this. In general, rather than trimming data that you don't want, avoid writing it in the first place. If you don't want the newline in the buffer, don't use fgets. Instead, use getc
or fgetc
or scanf
. Perhaps something like:
#include <stdio.h>
#include <stdlib.h>
int
main(void)
{
char Name[256];
char fmt[32];
int rc;
sprintf(fmt, "%%%zd[^\n]", sizeof Name - 1);
if( (rc = scanf(fmt, Name)) == 1 ) {
printf("Name = %s\n", Name);
}
return rc == 1 ? EXIT_SUCCESS : EXIT_FAILURE;
}