Topic: istream issue--should this work?
Author: drogers@grizzly.cs.washington.edu (David Rogers)
Date: 1995/07/14 Raw View
[ Article crossposted from comp.lang.c++ ]
[ Author was David Rogers ]
[ Posted on Sun, 9 Jul 1995 18:46:43 GMT ]
/* istrstream test program
* Dave Rogers - drogers@cs.washington.edu
* 7/9/95
* This toy program demonstrates what I find to be an anomoly
* with istreams: the extraction operator for an istream and a
* char * does not stop for a null character in the stream.
*
* Borland's implementation blasts through the null character until
* it hits the end of the buffer or a whitespace character. Is this
* the norm?
*
* Can anyone explain why this behavior is desireable? This doesn't
* happen with the other extraction operators. For the record,
* I think that a null should terminate extraction with the null,
* just as an extraction of an int terminates with the first non-digit.
* Subsequent extraction attempts would lead to fail begin set, just as
* with an integer extraction encountering an initial non-digit.
*
* I was unable to find this issue adressed in the April 28th WP or in the
* FAQ. I think it will arise frequently enough that a "standard behavior"
* should be defined and documented.
*/
#include <strstream.h>
int main(int, char*[])
{
// establish a line buffer
const int BUF_SIZE = 128;
char buffer[BUF_SIZE+1];
// and a string buffer
char string[BUF_SIZE+1];
// and an istrstream for incore formatting
istrstream source(buffer, BUF_SIZE);
while (cin) {
cin.getline(buffer, BUF_SIZE);
// now that buffer is loaded, clear any old errors
// and reseek to start of buffer
source.clear(0);
source.seekg(0, ios::beg);
while (source) {
// int string; // uncomment to compare behavior
if (source >> string) cout << string << endl;
}
}
return 0;
}
Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1995/07/15 Raw View
drogers@grizzly.cs.washington.edu (David Rogers) writes:
> * This toy program demonstrates what I find to be an anomoly
> * with istreams: the extraction operator for an istream and a
> * char * does not stop for a null character in the stream.
> *
> * Borland's implementation blasts through the null character until
> * it hits the end of the buffer or a whitespace character. Is this
> * the norm?
Yes. The extractor for char* stops on whitespace, and a null
character is not whitespace. If you want to stop on a null (or
a comma, or any other non-whitespace character), you will have to
use some other extraction method. You have several choices.
A null character terminates a literal character array, and is
used as a terminator for the C library string functions, but
has no special significance in a file.
--
Steve Clamage, stephen.clamage@eng.sun.com