stringstream::get?



 DEVELOP > c-Plus-Plus > stringstream::get?

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "David Olsson"
Date: 10 Dec 2004 07:18:26 AM
Object: stringstream::get?
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
.

User: "Matthias =?ISO-8859-1?Q?K=E4ppler?="

Title: Re: stringstream::get? 10 Dec 2004 08:12:20 AM
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

.

User: "Tom Widmer"

Title: Re: stringstream::get? 10 Dec 2004 10:16:49 AM
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
.

User: "rossum"

Title: Re: stringstream::get? 10 Dec 2004 06:50:54 PM
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
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER