I did! :headpalm:
I apologize. It is a typo; the code should of course be
size_t len;
len = strlen(line);
while (len > 0 && (line[len-1] == '\n' || line[len-1] == '\r'))
len--;
Type: Posts; User: Nominal Animal
I did! :headpalm:
I apologize. It is a typo; the code should of course be
size_t len;
len = strlen(line);
while (len > 0 && (line[len-1] == '\n' || line[len-1] == '\r'))
len--;
The suggestion by Adak causes a buffer underrun bug. It reads a byte just before the buffer, and if that byte equals 10, it replaces it with a zero.
My suggestion does not have that bug. I would...
Embedded nulls, in my experience, are almost always user errors: reading the wrong file, or such. It does not matter that fgets() is the wrong tool to read such input.
I'm actually quite upset...
If line does not contain a CR or an LF, then strcspn(line, "\r\n") == strlen(line).
So, what's the problem? Edit: oh, okay: no problem as such, just a different preference.
strchr() is...
To remove the newline (LF or CR LF) at the end of the line, I recommend
line[strcspn(line, "\r\n")] = '\0';
If line is a pointer to the buffer (not the buffer itself), you can use
line...