Re: sorting the input
- From: Ben Bacarisse <ben.usenet@xxxxxxxxx>
- Date: Sat, 26 Apr 2008 11:26:00 +0100
Barry Schwarz <schwarzb@xxxxxxxx> writes:
On Fri, 25 Apr 2008 14:26:35 +0100, Ben Bacarisse
<ben.usenet@xxxxxxxxx> wrote:
Barry Schwarz <schwarzb@xxxxxxxx> writes:
<snip>
This in no way affects the portable behavior of fgets. If the amount
of data in the "line" leaves at least two unused bytes in the buffer,
fgets MUST terminate the string with a '\n' followed by a '\0'.
Failure to do so violates the required performance specified in the
standard.
Not by my reading of it. It seems entirely within the allowed
behaviour that the last line (i.e. the one that triggers the
end-of-file condition) might not leave a \n in the buffer. In fact,
both reading a newline and seeing the end of the file are listed as
reasons to stop input and null-terminate the string. Am I missing
something?
I think we are using the term buffer in two different senses. I used
buffer to mean the "array" pointed to by the first argument to fgets.
You appear to be using it to refer to the I/O buffer used to manage
the stream. In that case I agree with you. I make no claim as to the
contents of the stream buffer.
No, I am talking about the same buffer as you. I, too, make no claims
about any intermediate stream buffers.
What I do claim is that santosh's
assertion that two end of file sequences (whatever that is)
[It is just how some systems let you signal an end-of-file when the
preceding input is not a newline. Thus, on Linux, to give a program a
three-byte input stream consisting of 'a', 'b' and 'c' you would type
"abc^D^D" at it.]
can
prevent fgets from storing a '\n' in the array buffer would a
violation of the standard. Ignoring error conditions, if the number
of bytes read is at least two less than the second argument to fgets,
then fgets must store both '\n' and '\0' in sequence immediately
following the last byte read. Failure to do so is a violation of its
behavior mandate.
All I can say is that is not how I read that part of the standard.
The governing text seems to be section 7.19.7.2 para. 2:
"The fgets function reads at most one less than the number of
characters specified by n from the stream pointed to by stream into
the array pointed to by s. No additional characters are read after a
new-line character (which is retained) or after end-of-file. A null
character is written immediately after the last character read into
the array."
which I can't read as mandating a newline being added to the buffer
unless one was there in the first place. The only place where there
may not be one is of course, by definition, the last line.
If you are right, and there are further restrictions that mandate a
newline character (space permitting) in the last successful return
from fgets, then gcc's fgets is not conforming (as are several other C
libraries I have used before but no longer have access to).
Of course, it is permissible for an implementation to require that the
last line of all text streams include a newline. So the behaviour you
say is required by the standard may be imposed by an implementation;
but this is "optional" and, in any case, would only apply to text
streams.
--
Ben.
.
- Follow-Ups:
- Re: sorting the input
- From: Barry Schwarz
- Re: sorting the input
- References:
- sorting the input
- From: arnuld
- Re: sorting the input
- From: arnuld
- Re: sorting the input
- From: Ben Bacarisse
- Re: sorting the input
- From: arnuld
- Re: sorting the input
- From: Ben Bacarisse
- Re: sorting the input
- From: arnuld
- Re: sorting the input
- From: Barry Schwarz
- Re: sorting the input
- From: arnuld
- Re: sorting the input
- From: Barry Schwarz
- Re: sorting the input
- From: santosh
- Re: sorting the input
- From: Barry Schwarz
- Re: sorting the input
- From: Ben Bacarisse
- Re: sorting the input
- From: Barry Schwarz
- sorting the input
- Prev by Date: Re: Another dynamic memory doubt
- Next by Date: Re: Garbage collection
- Previous by thread: Re: sorting the input
- Next by thread: Re: sorting the input
- Index(es):
Relevant Pages
|