I\'ve been doing a fairly easy program of converting a string of Characters (assuming numbers are entered) to an Integer.
After I was done, I noticed some very pecul
Yes, you want to avoid gets
. fgets
will always read the new-line if the buffer was big enough to hold it (which lets you know when the buffer was too small and there's more of the line waiting to be read). If you want something like fgets
that won't read the new-line (losing that indication of a too-small buffer) you can use fscanf
with a scan-set conversion like: "%N[^\n]"
, where the 'N' is replaced by the buffer size - 1.
One easy (if strange) way to remove the trailing new-line from a buffer after reading with fgets
is: strtok(buffer, "\n");
This isn't how strtok
is intended to be used, but I've used it this way more often than in the intended fashion (which I generally avoid).