I'm trying to write an inputted string elsewhere and do not know how to do away with the new line that appears as part of this string that I acquire with stdin and fgets.
char buffer[100];
memset(buffer, 0, 100);
fgets(buffer, 100, stdin);
printf("buffer is: %s\n stop",buffer);
I tried to limit the amount of data that fgets gets as well as limiting how much of the data is written but the new line remains. How can I simply get the inputted string up to the last character written with nothing else?
scanf("%99[^\n]%*c", buffer);
– Termitariumscanf("%99[^\n]%*c", buffer)
will not read anything intobuffer
if the firstchar
is'\n'
. The'\n'
remains instdin
. – Yachtsman'\n'
in the input stream, or do you want to read a line including the'\n'
and then discard the'\n'
? (This will affect the next input operation, which will or will not see the'\n'
waiting for it.) – Commitment