stringstream::get?

D

David Olsson

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
 
M

Matthias =?ISO-8859-1?Q?K=E4ppler?=

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 ','.
 
T

Tom Widmer

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
 
R

rossum

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
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top