Re: removing newline character from the buffer read by fgets
- From: Richard Heathfield <rjh@xxxxxxxxxxxxxxx>
- Date: Tue, 28 Nov 2006 13:01:38 +0000
junky_fellow@xxxxxxxxxxx said:
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 bytesmight reasonably reserve 0 for an exceptional condition, leaving the user
read.That is a reasonable approach. I would suggest a size_t type for
this. You
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.
0, if you're using an unsigned type such as size_t.
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".
Why not change n's type to size_t? After all, it represents the size of an
object, does it not?
Is there any special reason for using size_t for "bytes_read" ?
It's an unsigned integer type that is guaranteed to be able to store the
largest possible object size, whereas int is not.
See also the following standard library functions, which use size_t for much
the same reason:
bsearch, calloc, fread, fwrite, malloc, memchr, memcmp, memcpy, memmove,
memset, qsort, realloc, setvbuf, strftime, strlen, strncat, strncmp,
strncpy, and no doubt I missed one or two.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
.
- References:
- removing newline character from the buffer read by fgets
- From: junky_fellow@xxxxxxxxxxx
- Re: removing newline character from the buffer read by fgets
- From: Richard Heathfield
- Re: removing newline character from the buffer read by fgets
- From: junky_fellow@xxxxxxxxxxx
- Re: removing newline character from the buffer read by fgets
- From: Richard Heathfield
- Re: removing newline character from the buffer read by fgets
- From: junky_fellow@xxxxxxxxxxx
- Re: removing newline character from the buffer read by fgets
- From: Richard Heathfield
- Re: removing newline character from the buffer read by fgets
- From: junky_fellow@xxxxxxxxxxx
- removing newline character from the buffer read by fgets
- Prev by Date: Re: Reading a string of unknown size
- Next by Date: Re: Urgently required
- Previous by thread: Re: removing newline character from the buffer read by fgets
- Next by thread: Re: removing newline character from the buffer read by fgets
- Index(es):
Relevant Pages
|