 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Chris Vine Guest
|
Posted: Tue Dec 14, 2004 3:32 am Post subject: return value of std::streambuf::xsgetn() |
|
|
The documentation I have seen for std::streambuf::xsgetn() describes it as
returning the number of characters fetched. On the other hand I see its
return type is std::streamsize, which implies it is a signed type capable
of returning EOF.
When overriding xsgetn() on a stream buffer deriving from std::streambuf,
and where a call to xsgetn() results in no characters being fetched because
the buffer is empty and a system read fails - say Unix read() returns 0 for
end of file or -1 for error, is xsgetn() expected to return 0 or is it
expected to signal a stream failure by returning EOF?
Chris.
--
To reply by e-mail, remove the "--nospam--" in the address
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Tue Dec 14, 2004 8:35 pm Post subject: Re: return value of std::streambuf::xsgetn() |
|
|
Chris Vine wrote:
| Quote: | The documentation I have seen for std::streambuf::xsgetn() describes
it as returning the number of characters fetched. On the other hand
I
see its return type is std::streamsize, which implies it is a signed
type capable of returning EOF.
When overriding xsgetn() on a stream buffer deriving from
std::streambuf, and where a call to xsgetn() results in no characters
being fetched because the buffer is empty and a system read fails -
say Unix read() returns 0 for end of file or -1 for error, is
xsgetn()
expected to return 0 or is it expected to signal a stream failure by
returning EOF?
|
The standard says nothing about xsgetn() ever returning EOF.
In general, the standard does not allow distinguishing between errors
and a real end of file on input. In other words, an input stream is
required to behave in the same way when read returns 0 as when it
returns -1. In this case, my reading of the standard is that xsgetn()
must return 0, never EOF.
--
James Kanze GABI Software http://www.gabi-soft.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Paolo Carlini Guest
|
Posted: Tue Dec 14, 2004 8:37 pm Post subject: Re: return value of std::streambuf::xsgetn() |
|
|
Chris Vine wrote:
| Quote: | The documentation I have seen for std::streambuf::xsgetn() describes it as
returning the number of characters fetched. On the other hand I see its
return type is std::streamsize, which implies it is a signed type capable
of returning EOF.
|
Actually, you can find streamsize *everywhere* in the library, and the
proper type for returning traits::eof() is int_type, not streamsize
(21.1.2). Compare to, e.g, sbumpc().
Paolo.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Chris Vine Guest
|
Posted: Wed Dec 15, 2004 1:39 am Post subject: Re: return value of std::streambuf::xsgetn() |
|
|
[email]kanze (AT) gabi-soft (DOT) fr[/email] wrote:
| Quote: | Chris Vine wrote:
The documentation I have seen for std::streambuf::xsgetn() describes
it as returning the number of characters fetched. On the other hand
I
see its return type is std::streamsize, which implies it is a signed
type capable of returning EOF.
When overriding xsgetn() on a stream buffer deriving from
std::streambuf, and where a call to xsgetn() results in no characters
being fetched because the buffer is empty and a system read fails -
say Unix read() returns 0 for end of file or -1 for error, is
xsgetn()
expected to return 0 or is it expected to signal a stream failure by
returning EOF?
The standard says nothing about xsgetn() ever returning EOF.
In general, the standard does not allow distinguishing between errors
and a real end of file on input. In other words, an input stream is
required to behave in the same way when read returns 0 as when it
returns -1. In this case, my reading of the standard is that xsgetn()
must return 0, never EOF.
|
Thanks for that. And the same is true of output (xsputn())?
Chris.
--
To reply by e-mail, remove the "--nospam--" in the address
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Wed Dec 15, 2004 4:39 pm Post subject: Re: return value of std::streambuf::xsgetn() |
|
|
Chris Vine wrote:
| Quote: | kanze (AT) gabi-soft (DOT) fr wrote:
Chris Vine wrote:
The documentation I have seen for std::streambuf::xsgetn()
describes it as returning the number of characters fetched. On
the
other hand I see its return type is std::streamsize, which implies
it is a signed type capable of returning EOF.
When overriding xsgetn() on a stream buffer deriving from
std::streambuf, and where a call to xsgetn() results in no
characters being fetched because the buffer is empty and a system
read fails - say Unix read() returns 0 for end of file or -1 for
error, is xsgetn() expected to return 0 or is it expected to
signal
a stream failure by returning EOF?
The standard says nothing about xsgetn() ever returning EOF.
In general, the standard does not allow distinguishing between
errors and a real end of file on input. In other words, an input
stream is required to behave in the same way when read returns 0 as
when it returns -1. In this case, my reading of the standard is
that xsgetn() must return 0, never EOF.
Thanks for that. And the same is true of output (xsputn())?
|
Pretty much so. In general, the standard provides no way for a
streambuf to distinguish between end of file and an error condition;
this is definitly the case for functions like sbumpc or sgetc, but also
for xsputn and xsgetn, according to my reading of the standard. An
attempted read or write either works, or it doesn't, for reasons
presumably unknown.
Given this, if an attempted read doesn't work, istream supposes EOF and
sets eofbit, and if this means we didn't read the requested data,
failbit. If an attempted write doesn't work, ostream supposes an
error,
and sets badbit. This is right most of the time, when readig to and
writing from a disk. It's wrong if you have a hard read error on the
disk, however, and it is wrong for other types of output, where the
target has a fixed length.
This is a know weakness of the iostreams. If you need more
information,
the only solution I know of is to add special functions to your derived
streambuf, then dynamic_cast<> the results of rdbuf() to get at them.
--
James Kanze GABI Software http://www.gabi-soft.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|