home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!stanford.edu!lucid.com!lucid.com!jss
- From: jss@lucid.com (Jerry Schwarz)
- Subject: Re: iostreams bug in BC?
- Message-ID: <1992Nov19.203652.3656@lucid.com>
- Keywords: whitespace; stream; borland; cfront; fail
- Sender: usenet@lucid.com
- Reply-To: jss@lucid.com (Jerry Schwarz)
- Organization: Lucid, Inc.
- References: <1992Nov18.154925.5930@siesoft.co.uk>
- Date: Thu, 19 Nov 92 20:36:52 GMT
- Lines: 48
-
- In article <1992Nov18.154925.5930@siesoft.co.uk>, huw@siesoft.co.uk (Huw Roberts) writes:
- |> I have a problem with BC++ 3.1. I'm not sure what the correct behaviour is
- |> so I can't tell whether or not it's a bug. The program certainly behaves
- |> differently on cfront:
- |>
- |> ...
- |> istrstream fred (" 35 ");
- |> int x;
- |> fred >> x >> ws;
- |> if (fred.fail())
- |> cout << "You're kidding!`n";
- |>
- |> Borland produces the message, cfront does not.
- |>
-
- Unfortunately all the original iostream "specification" said
- was that ws "extracts whitespace". It did not specify what
- should happen if it encounters the end of file while doing
- so. Some implementers decided it should set an error bit.
-
- The current words in the x3j16/sc22 working paper say
- that ws is not supposed to set failbit when it encounters
- EOF.
-
- The workaround is to write your own function. I have attached
- below an "industrial strength" version.
-
- -- Jerry Schwarz
- ------------------------------------------
-
- #include <ctype.h>
- #include <iostream.h>
-
-
- istream& myws(istream& in) {
- if ( !in.ipfx(1) ) return in ;
- streambuf* sb = in.rdbuf() ;
- for ( int c = sb->sgetc() ;
- c != EOF && isspace(c) ;
- c = sb->snextc() ) {
- /* do nothing */
- }
- if ( c==EOF ) {
- in.clear(in.rdstate()|ios::eofbit) ;
- }
- in.isfx();
- return in ;
- }
-