 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Ralf Fassel Guest
|
Posted: Fri Sep 10, 2004 2:00 am Post subject: reading N chars from istream into stringstream |
|
|
I hoped to find this in the FAQ (lite or book), but didn't.
I need to read N chars from an istream into an stringstream for
further line-by-line processing. The N bytes are supposed to be text
lines, but I cannot directly operate on the lines in the istream via
getline(), since the last line might not have a line separator but
runs directly into binary data.
Right now I do (error and stream state checks omitted)
char buffer[buflen];
istream.read(buffer, buflen);
stringstream.str(std::string(buffer, istream.gcount()));
// now we operate via getline() on the stringstream
Is there a better way? Ideally I would like to omit the temporary
string object and read the buflen bytes into the stringstream
directly. I also tried
istream >> noskipws >> setw(buflen) >> stringstream.rdbuf();
but this reads the whole istream, and I did not find a way to limit to
buflen characters. The setw() obviously only takes effect when
reading into char[].
istream.get(streambuf, size) stops at newlines or delimiters, which I
also don't want here.
Thanks
R'
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Gerhard Wesp Guest
|
Posted: Wed Oct 13, 2004 10:10 pm Post subject: Re: reading N chars from istream into stringstream |
|
|
Ralf Fassel <ralfixx (AT) gmx (DOT) de> wrote:
| Quote: | Is there a better way? Ideally I would like to omit the temporary
string object and read the buflen bytes into the stringstream
directly. I also tried
|
I can't think of any. These kinds of questions seem to occur quite
frequently with string streams. The reason is IMHO a design flaw in the
library---the string buffer should allow direct access to its underlying
string, it would just act as a proxy on it. Any known discussions on
this issue in the past?
Cheers
-Gerhard
--
Gerhard Wesp o o Tel.: +41 (0) 43 5347636
Bachtobelstrasse 56 | http://www.cosy.sbg.ac.at/~gwesp/
CH-8045 Zuerich _/ See homepage for email address!
[ 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
|
|