c – Removing trailing newline character from fgets() input
c – Removing trailing newline character from fgets() input
Perhaps the simplest solution uses one of my favorite little-known functions, strcspn()
:
buffer[strcspn(buffer, n)] = 0;
If you want it to also handle r
(say, if the stream is binary):
buffer[strcspn(buffer, rn)] = 0; // works for LF, CR, CRLF, LFCR, ...
The function counts the number of characters until it hits a r
or a n
(in other words, it finds the first r
or n
). If it doesnt hit anything, it stops at the