stringstream::get?

Discussion in 'C++' started by David Olsson, Dec 10, 2004.

  1. David Olsson

    David Olsson Guest

    Hello!

    I have a little problem with one of the get methods of the
    stringstream class. Consider the code:

    #include <iostream>
    #include <sstream>
    #include <string>

    using namespace std;

    int main(int argc, char *argv[]) {
    string all_names = "David Olsson, Bengt Bengtsson, Kalle von Sydow";
    stringstream ns(all_names);
    stringbuf name_buf;
    while(ns.get(name_buf, ',')) {
    string name = name_buf.str();
    cout << name << endl;
    }

    return(EXIT_SUCCESS);
    }

    I would expect this code to produce the following output:

    David Olsson
    Bengt Bengtsson
    Kalle von Sydow

    However, the real output is as follows:

    David Olsson

    The problem is thus that the second call to ns.get(...) fails, even
    though there still should be characters to read.

    Am I using the stringstream class in an appropriate way? Very grateful
    for any suggestions!
    David Olsson
     
    David Olsson, Dec 10, 2004
    #1
    1. Advertising

  2. From the official documentation:

    <snip>
    Characters are extracted and inserted into sb until one of the following
    happens:

    * the input sequence reaches EOF
    * insertion into the output buffer fails (in this case, the character
    that would have been inserted is not extracted)
    * the next character equals delim (in this case, the character is not
    extracted)
    * an exception occurs (and in this case is caught)
    </snip>

    That means, in your case, the string is read until the first ','.

    David Olsson wrote:

    > Hello!
    >
    > I have a little problem with one of the get methods of the
    > stringstream class. Consider the code:
    >
    > #include <iostream>
    > #include <sstream>
    > #include <string>
    >
    > using namespace std;
    >
    > int main(int argc, char *argv[]) {
    > string all_names = "David Olsson, Bengt Bengtsson, Kalle von Sydow";
    > stringstream ns(all_names);
    > stringbuf name_buf;
    > while(ns.get(name_buf, ',')) {
    > string name = name_buf.str();
    > cout << name << endl;
    > }
    >
    > return(EXIT_SUCCESS);
    > }
    >
    > I would expect this code to produce the following output:
    >
    > David Olsson
    > Bengt Bengtsson
    > Kalle von Sydow
    >
    > However, the real output is as follows:
    >
    > David Olsson
    >
    > The problem is thus that the second call to ns.get(...) fails, even
    > though there still should be characters to read.
    >
    > Am I using the stringstream class in an appropriate way? Very grateful
    > for any suggestions!
    > David Olsson
     
    Matthias =?ISO-8859-1?Q?K=E4ppler?=, Dec 10, 2004
    #2
    1. Advertising

  3. David Olsson

    Tom Widmer Guest

    On 10 Dec 2004 05:18:26 -0800, (David Olsson)
    wrote:

    >Hello!
    >
    >I have a little problem with one of the get methods of the
    >stringstream class. Consider the code:
    >
    >#include <iostream>
    >#include <sstream>
    >#include <string>
    >
    >using namespace std;
    >
    >int main(int argc, char *argv[]) {
    > string all_names = "David Olsson, Bengt Bengtsson, Kalle von Sydow";
    > stringstream ns(all_names);
    > stringbuf name_buf;
    > while(ns.get(name_buf, ',')) {
    > string name = name_buf.str();
    > cout << name << endl;
    > }
    >
    > return(EXIT_SUCCESS);
    >}
    >
    >I would expect this code to produce the following output:
    >
    >David Olsson
    >Bengt Bengtsson
    >Kalle von Sydow
    >
    >However, the real output is as follows:
    >
    >David Olsson
    >
    >The problem is thus that the second call to ns.get(...) fails, even
    >though there still should be characters to read.
    >
    >Am I using the stringstream class in an appropriate way? Very grateful
    >for any suggestions!


    The get function leaves the ',' character in the stream, so the next
    call to get just finds that (and thus sets failbit on ns, since no
    characters were extracted).

    Instead, you should do something like:

    #include <iostream>
    #include <sstream>
    #include <string>

    using namespace std;

    int main(int argc, char *argv[]) {
    string all_names =
    "David Olsson, Bengt Bengtsson, Kalle von Sydow";
    stringstream ns(all_names);
    string name;
    while(getline(ns, name, ','))
    cout << name << endl;
    if (!name.empty()) //did extract something at least
    cout << name << endl;

    return(EXIT_SUCCESS);
    }

    Tom
     
    Tom Widmer, Dec 10, 2004
    #3
  4. David Olsson

    rossum Guest

    On 10 Dec 2004 05:18:26 -0800, (David Olsson)
    wrote:

    >Hello!
    >
    >I have a little problem with one of the get methods of the
    >stringstream class. Consider the code:


    As Matthias and Tom have explained, get() stops when it encounters the
    delimiter, a comma in this case. Tom has provided one solution - use
    getline() instead. If you want to stick with get() then a solution is
    to skip over the comma by using ignore(). This lets get() continue
    normally from after the comma.

    This immediately reveals a second problem - you get more than before,
    but you still don't get what you were expecting. I will leave you to
    solve that one yourself.

    rossum

    >
    >#include <iostream>
    >#include <sstream>
    >#include <string>
    >
    >using namespace std;
    >
    >int main(int argc, char *argv[]) {
    > string all_names = "David Olsson, Bengt Bengtsson, Kalle von Sydow";
    > stringstream ns(all_names);
    > stringbuf name_buf;
    > while(ns.get(name_buf, ',')) {
    > string name = name_buf.str();

    ns.ignore(); // Skips over delimiting comma
    > cout << name << endl;
    > }
    >
    > return(EXIT_SUCCESS);
    >}
    >
    >I would expect this code to produce the following output:
    >
    >David Olsson
    >Bengt Bengtsson
    >Kalle von Sydow
    >
    >However, the real output is as follows:
    >
    >David Olsson
    >
    >The problem is thus that the second call to ns.get(...) fails, even
    >though there still should be characters to read.
    >
    >Am I using the stringstream class in an appropriate way? Very grateful
    >for any suggestions!
    >David Olsson


    --

    The ultimate truth is that there is no Ultimate Truth
     
    rossum, Dec 11, 2004
    #4
    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. Marc Schellens

    stringstream

    Marc Schellens, Jul 1, 2003, in forum: C++
    Replies:
    3
    Views:
    534
  2. minjie
    Replies:
    1
    Views:
    663
    Victor Bazarov
    Jul 15, 2003
  3. Bill Beacom

    stringstream problem

    Bill Beacom, Sep 15, 2003, in forum: C++
    Replies:
    2
    Views:
    465
    Peter Kragh
    Sep 15, 2003
  4. Ellarco
    Replies:
    5
    Views:
    4,537
    Ellarco
    Sep 24, 2003
  5. Medi Montaseri

    How to shrink a stringstream

    Medi Montaseri, Sep 25, 2003, in forum: C++
    Replies:
    2
    Views:
    379
    Medi Montaseri
    Sep 26, 2003
Loading...

Share This Page