Re: removing newline character from the buffer read by fgets





On Nov 28, 3:27 pm, Richard Heathfield <r...@xxxxxxxxxxxxxxx> wrote:
junky_fel...@xxxxxxxxxxx said:

On Nov 28, 1:59 pm, Richard Heathfield wrote:
The principal objection I have to your code is that it fails to provide a
way to distinguish between a line that has been read completely and a
line that has been read only partially because of insufficient space in
the buffer.

For that I would change the function to return the number of bytes
read.That is a reasonable approach. I would suggest a size_t type for this. You
might reasonably reserve 0 for an exceptional condition, leaving the user
to enquire of ferror() to find out whether Something Really Bad happened or
whether the routine simply couldn't find any data to stuff into the buffer
(i.e. encountered EOF on the first getc call).


Using 0 or some other value (negative value) for an exceptional
condition
is a good idea. thanks for that.
Regarding "size_t", I used the type "int" for "bytes_read" as its value
will
never be greater than "n". The type of "n" is "int", so I thought
"bytes_read"
should also be "int".

Is there any special reason for using size_t for "bytes_read" ?

.