C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

reading N chars from istream into stringstream

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Ralf Fassel
Guest





PostPosted: Fri Sep 10, 2004 2:00 am    Post subject: reading N chars from istream into stringstream Reply with quote



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





PostPosted: Wed Oct 13, 2004 10:10 pm    Post subject: Re: reading N chars from istream into stringstream Reply with quote



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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.