Perhaps the simplest solution uses one of my favorite little-known functions, strcspn()
:
buffer[strcspn(buffer, "
")] = 0;
If you want it to also handle '
'
(say, if the stream is binary):
buffer[strcspn(buffer, "
")] = 0; // works for LF, CR, CRLF, LFCR, ...
The function counts the number of characters until it hits a '
'
or a '
'
(in other words, it finds the first '
'
or '
'
). If it doesn't hit anything, it stops at the ''
(returning the length of the string).
Note that this works fine even if there is no newline, because strcspn
stops at a ''
. In that case, the entire line is simply replacing ''
with ''
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…