Article View: comp.std.c++
Article #3623Re: Streams and eof()
From: J. Kanze
Date: Fri, 08 Mar 1996 17:08
Date: Fri, 08 Mar 1996 17:08
54 lines
3459 bytes
3459 bytes
In article <4hkean$mrc@engnews1.Eng.Sun.COM> clamage@Eng.Sun.COM (Steve
Clamage) writes:
|> No, it must work. EOF is required to have a value different from every
|> character value.
Where does it say this? (It's what I always thought too, until a recent
discussion in comp.std.c proved me otherwise, at least for C.)
It is indirectly required if INT_MAX >= UCHAR_MAX, at least for fgetc in
C, but if int's and char's are the same size, all bets are off.
By `character value', I presume you mean a value that can be returned by
a successful read, since the original question concerned using
istream::peek and istream::get for binary input. Although even for
character (non-binary) input, I can see no guarantees once one leaves
the "C" locale.
|> The character-reading functions that can return either
|> a character or EOF return a type that that can represent the EOF value
|> as distinct from any character value.
This is definitly not the case in ISO C, and I can find no such
guarantee in the current draft either, although in this case, it might
be a simple oversight. Again, there is some ambiguity in your use of
the word character: imagine a 16 bit machine with sizeof( int ) ==
sizeof( char ) == 1, using Unicode as its native character code.
Unicode explicitly doesn't use 0xFFFF (-1) as a legal code, so if EOF is
-1 (a frequent implementation), there is no problem with reading
`characters'. Reading a `char' from a file opened in binary mode is
still a problem, however.
|> In a traditional implementation, EOF has the int value -1, and characters
|> have 8-bit values. The peek function returns the character value promoted
|> to type int without sign extension, as if by the cast sequence
|> (int)(unsigned char)the_value
|> Such a value cannot compare equal to the int value -1.
I suspect that a lot of code developed on a traditional implementation
will break on a machine where all integral types are the same length.
(I know that a lot of mine would. Even in the parts I thought I wrote
portably.)
--
James Kanze (+33) 88 14 49 00 email: kanze@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs Bourgeois, 67000 Strasbourg, France
Conseils, études et réalisations en logiciel orienté objet --
-- A la recherche d'une activité dans une region francophone
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Technical Details
Message-ID:
<KANZE.96Mar8124126@gabi.gabi-soft.fr>Path:
arc1-ca.pugleaf.net!chronicle.mti.sgi.com!austernReferences:
<4hgap6$6o7@fu-berlin.de> <4hkean$mrc@engnews1.Eng.Sun.COM>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!chronicle.mti.sgi.com!austern From: kanze@gabi-soft.fr (J. Kanze) Newsgroups: comp.std.c++ Subject: Re: Streams and eof() Date: 08 Mar 1996 09:08:56 PST Organization: GABI Software, Sarl. Approved: austern@isolde.mti.sgi.com Message-ID: <KANZE.96Mar8124126@gabi.gabi-soft.fr> References: <4hgap6$6o7@fu-berlin.de> <4hkean$mrc@engnews1.Eng.Sun.COM> NNTP-Posting-Host: isolde.mti.sgi.com X-Original-Date: 08 Mar 1996 11:41:26 GMT In-reply-to: clamage@Eng.Sun.COM's message of 07 Mar 96 13:40:54 GMT X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBVAwUBMUBpuEy4NqrwXLNJAQHkFAH/eWdYdZrolAeCL59HFgV4TYX8D8dkximn D4XvlT8tEhZf/KqC25fmNpW4zCALHTQ90w+WPuIcRLYqkq3oGqG4BA== =oxej Originator: austern@isolde.mti.sgi.com