Using basic_istream& get( basic_streambuf....

Discussion in 'C++' started by DeveloperDave, Jan 26, 2011.

  1. 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
    DeveloperDave, Jan 26, 2011
    #1
    1. Advertising

  2. 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.
    DeveloperDave, Jan 26, 2011
    #2
    1. Advertising

  3. 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
    Vaclav Haisman, Jan 26, 2011
    #3
  4. 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
    Vaclav Haisman, Jan 26, 2011
    #4
  5. On Jan 26, 6:44 pm, Vaclav Haisman <> 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
    DeveloperDave, Jan 27, 2011
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guest
    Replies:
    3
    Views:
    706
    John Harrison
    Aug 8, 2004
  2. Guest
    Replies:
    0
    Views:
    389
    Guest
    Aug 28, 2004
  3. Guest
    Replies:
    1
    Views:
    553
    Jonathan Turkanis
    Aug 29, 2004
  4. Steven T. Hatton

    I/O std::basic_streambuf put and get areas?

    Steven T. Hatton, Jul 3, 2005, in forum: C++
    Replies:
    3
    Views:
    511
    Steven T. Hatton
    Jul 4, 2005
  5. Bit Byter
    Replies:
    2
    Views:
    699
    Tom Widmer
    Apr 13, 2006
Loading...

Share This Page