Using basic_istream& get( basic_streambuf....

D

DeveloperDave

Hi,

Using C++ I am trying to read x number of characters up to a delimiter
character and store them in a istringstream.

I am using the ifstream class to open a file.
Looking at the API I noticed a 'get' operator that took a reference to
a basic_streambuf as a parameter.
see http://www.cppreference.com/wiki/io...ww.cppreference.com/wiki/io/basic_istream/get

Anyway I was wondering is it possible to create a istringstream and
then get the underlying basic_streambuf, and pass it into this get
method.

I am struggling with the syntax, so if this is possible and someone
has an example I would appreciate it.

Cheers
 
D

DeveloperDave

I should mention I've tried:

<code>
ifstream inputStream(filename.c_str(), ios_base::in);
while(inputStream.good())
{
istringstream buffer;
stringbuf* stringBuffer = buffer.rdbuf();
streambuf* streamBuffer = stringBuffer;

inputStream.get(*streamBuffer, ',');
cout << "Got: " << buffer.str() << endl;
}
</code>

This compiles, but unfortunately doesn't seem to find the delimiter
character.
 
V

Vaclav Haisman

DeveloperDave wrote, On 26.1.2011 18:36:
I should mention I've tried:

<code>
ifstream inputStream(filename.c_str(), ios_base::in);
while(inputStream.good())
{
istringstream buffer;
stringbuf* stringBuffer = buffer.rdbuf();
streambuf* streamBuffer = stringBuffer;

inputStream.get(*streamBuffer, ',');
You are insterting the read characters into a streambuf of input buffer,
which IMHO does not make much sense.
cout << "Got: " << buffer.str() << endl;
}
</code>

This compiles, but unfortunately doesn't seem to find the delimiter
character.
I have tried the following and it works as I would expect:

stringstream iss ("abc,efg,klm");
ostringstream oss;
streambuf * sb = oss.rdbuf ();
iss.get (*sb, ',');
cout << oss.str ();

It prints "abc".
 
V

Vaclav Haisman

Vaclav Haisman wrote, On 26.1.2011 19:37:
DeveloperDave wrote, On 26.1.2011 18:36:
You are insterting the read characters into a streambuf of input buffer,
which IMHO does not make much sense.

I have tried the following and it works as I would expect:

stringstream iss ("abc,efg,klm");
ostringstream oss;
streambuf * sb = oss.rdbuf ();
iss.get (*sb, ',');
cout << oss.str ();

It prints "abc".
I was probably too quick with the reply. If you want to do more input with
the piece that you read into the streambuf, you can use stringstream instead
of istringstream:

istringstream iss ("abc,efg,klm");
stringstream oss;
streambuf * sb = oss.rdbuf ();
iss.get (*sb, ',');
cout << oss.str () << " ";
string s;
getline (oss, s);
cout << s;

This prints "abc abc".
 
D

DeveloperDave

Vaclav Haisman wrote, On 26.1.2011 19:37:










I was probably too quick with the reply. If you want to do more input with
the piece that you read into the streambuf, you can use stringstream instead
of istringstream:

istringstream iss ("abc,efg,klm");
stringstream oss;
streambuf * sb = oss.rdbuf ();
iss.get (*sb, ',');
cout << oss.str () << " ";
string s;
getline (oss, s);
cout << s;

This prints "abc abc".

Thanks that works perfectly. I can see how I should be using that
class and method now.

Cheers
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top