Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Using basic_istream& get( basic_streambuf....

Reply
Thread Tools

Using basic_istream& get( basic_streambuf....

 
 
DeveloperDave
Guest
Posts: n/a
 
      01-26-2011
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/...ic_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



 
Reply With Quote
 
 
 
 
DeveloperDave
Guest
Posts: n/a
 
      01-26-2011
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.
 
Reply With Quote
 
 
 
 
Vaclav Haisman
Guest
Posts: n/a
 
      01-26-2011
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".

--
VH
 
Reply With Quote
 
Vaclav Haisman
Guest
Posts: n/a
 
      01-26-2011
Vaclav Haisman wrote, On 26.1.2011 19:37:
> 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".
>

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".

--
VH
 
Reply With Quote
 
DeveloperDave
Guest
Posts: n/a
 
      01-27-2011
On Jan 26, 6:44*pm, Vaclav Haisman <(E-Mail Removed)> wrote:
> Vaclav Haisman wrote, On 26.1.2011 19:37:
>
>
>
>
>
>
>
> > 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".

>
> 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".
>
> --
> VH


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

Cheers
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using a Link Button to redirect to another page by using data from =?Utf-8?B?R1REcml2ZXI=?= ASP .Net 1 02-16-2005 07:04 PM
Error page using the Application_Error void dosnt work when using DIV for a please wait message s_erez@hotmail.com ASP .Net 2 12-24-2004 12:11 PM
no code in webform using vs.net, but in webform using notepad timmso ASP .Net 1 12-12-2003 04:30 PM
Using GetOleDbSchemaTable to get SQL Server Field Description - using pete ASP .Net 1 08-29-2003 10:50 AM
Re: MVP? Index error on nested element using System.xml but NOT using msxml??? William F. Robertson, Jr. ASP .Net 1 06-25-2003 08:08 PM



Advertisments