home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / cplus / 16585 < prev    next >
Encoding:
Text File  |  1992-11-19  |  1.8 KB  |  62 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!stanford.edu!lucid.com!lucid.com!jss
  3. From: jss@lucid.com (Jerry Schwarz)
  4. Subject: Re: iostreams bug in BC?
  5. Message-ID: <1992Nov19.203652.3656@lucid.com>
  6. Keywords: whitespace; stream; borland; cfront; fail
  7. Sender: usenet@lucid.com
  8. Reply-To: jss@lucid.com (Jerry Schwarz)
  9. Organization: Lucid, Inc.
  10. References:  <1992Nov18.154925.5930@siesoft.co.uk>
  11. Date: Thu, 19 Nov 92 20:36:52 GMT
  12. Lines: 48
  13.  
  14. In article <1992Nov18.154925.5930@siesoft.co.uk>, huw@siesoft.co.uk (Huw Roberts) writes:
  15. |> I have a problem with BC++ 3.1.  I'm not sure what the correct behaviour is
  16. |> so I can't tell whether or not it's a bug.  The program certainly behaves
  17. |> differently on cfront:
  18. |> 
  19. |>     ...
  20. |>     istrstream fred (" 35    ");
  21. |>     int x;
  22. |>     fred >> x >> ws;
  23. |>     if (fred.fail())
  24. |>     cout << "You're kidding!`n";
  25. |> 
  26. |> Borland produces the message, cfront does not.
  27. |> 
  28.  
  29. Unfortunately all the original iostream "specification" said
  30. was that ws "extracts whitespace".  It did not specify what
  31. should happen if it encounters the end of file while doing
  32. so.   Some implementers decided it should set an error bit.
  33.  
  34. The current words in the x3j16/sc22 working paper say
  35. that ws is not supposed to set failbit when it encounters
  36. EOF.  
  37.  
  38. The workaround is to write your own function. I have attached
  39. below an "industrial strength" version.
  40.     
  41.     -- Jerry Schwarz
  42. ------------------------------------------
  43.  
  44. #include <ctype.h>
  45. #include <iostream.h>
  46.  
  47.  
  48. istream& myws(istream& in) {
  49.     if ( !in.ipfx(1) )  return in ;
  50.     streambuf* sb = in.rdbuf() ;
  51.     for ( int c = sb->sgetc() ; 
  52.           c != EOF && isspace(c) ; 
  53.           c = sb->snextc() ) {
  54.         /* do nothing */
  55.     } 
  56.     if ( c==EOF ) {
  57.         in.clear(in.rdstate()|ios::eofbit) ;
  58.     }
  59.     in.isfx();
  60.     return in ;
  61. }
  62.