Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > stringstream::get?

Reply
Thread Tools

stringstream::get?

 
 
David Olsson
Guest
Posts: n/a
 
      12-10-2004
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
 
Reply With Quote
 
 
 
 
Matthias =?ISO-8859-1?Q?K=E4ppler?=
Guest
Posts: n/a
 
      12-10-2004
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


 
Reply With Quote
 
 
 
 
Tom Widmer
Guest
Posts: n/a
 
      12-10-2004
On 10 Dec 2004 05:18:26 -0800, http://www.velocityreviews.com/forums/(E-Mail Removed) (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
 
Reply With Quote
 
rossum
Guest
Posts: n/a
 
      12-11-2004
On 10 Dec 2004 05:18:26 -0800, (E-Mail Removed) (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
 
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




Advertisments