473,804 Members | 4,066 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

stringstream::g et?

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(na me_buf, ',')) {
string name = name_buf.str();
cout << name << endl;
}

return(EXIT_SUC CESS);
}

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
Jul 22 '05 #1
3 4224
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(na me_buf, ',')) {
string name = name_buf.str();
cout << name << endl;
}

return(EXIT_SUC CESS);
}

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


Jul 22 '05 #2
On 10 Dec 2004 05:18:26 -0800, da**********@vi mio.com (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(na me_buf, ',')) {
string name = name_buf.str();
cout << name << endl;
}

return(EXIT_SUC CESS);
}

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(n s, name, ','))
cout << name << endl;
if (!name.empty()) //did extract something at least
cout << name << endl;

return(EXIT_SUC CESS);
}

Tom
Jul 22 '05 #3
On 10 Dec 2004 05:18:26 -0800, da**********@vi mio.com (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(na me_buf, ',')) {
string name = name_buf.str(); ns.ignore(); // Skips over delimiting comma cout << name << endl;
}

return(EXIT_SUC CESS);
}

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
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
5815
by: David Johnstone | last post by:
Hi Group, I am using gcc 3.2.2 on linux 2.4.19-4GB (SuSe 8.2). I have a situation where I need to tie an ostringstream to an istringstream so I can write to one and read back what I wrote from the other. I had expected the code to look like: ostringstream* ostr_stdin = new ostringstream();
1
4907
by: KidLogik | last post by:
Hello! I am using std::stringstream && std::string to parse a text file -> std::ifstream in; std::string s; std::streamstring ss; int n; I grab each line like so -> std::getline(in,s);
6
7970
by: Christopher Benson-Manica | last post by:
This is a subtle variation of a question I posted some time ago. #include <map> #include <iostream> #include <sstream> int main(void) { try { std::stringstream s;
5
817
by: William Payne | last post by:
How do you get rid the contents in a std::stringstream? I'm using one in a for loop to format some status messages which I print to the screen. The stringstream member function clear() only clears flags (bits) in the stream, right? I solved it by declaring the stringstream inside the loop body even though I have need of another stringstream just after the loop. So for each iteration the constructor of the stringstream will be executed. Is...
1
1868
by: Edward Diener | last post by:
If you run this simple console program after building it on VC++ .NET 2003, you will see the error message, "Error getting initial stream position", from the attempt to use the std::ostream::tellp() method to get the output stream position of a std::stringstream. I can see no reason for this error and deem it a bug in VC++ .NET 2003. If anyone knows of a workaround or a fix for this, I would appreciate finding out about it. BTW, where do I...
0
1531
by: avidamani | last post by:
Hi, I have a c++ application in which I am using stringstream to convert integer/long to string. std::stringstream x; int y = 5; x << y; Whenever I do that I get an unalignedd access error and my application
9
5527
by: martinezfive | last post by:
Hi, I feel no doubt some documentation contains my answer, so bare with me. Given the following: #inclde <stdio.h> #include <sstream> void f() { std::stringstream a("Hello World!\n");
0
1440
by: ziyanjoe | last post by:
Hi! I am writing a program for the robot that runs as a daemon on a linux machine. Since debug output cannot be seen on stdout, I want to create an iostream to handle all the output messages. However, I do not want to use a file or output to the clog since I want to achieve the debug message remotely through TCP connection. I've already built a TCP server for transmitting data, all i need is an "endless" stringstream. I tried to code this...
2
456
by: DaLoverhino | last post by:
I'm trying to get use to using stringstream and I've run across a case that puzzles me. Perhaps you have some suggestion for the following: Why in the code below for case 2, intVal is always 1? // StringToInt.cc // // #include <sstream>
0
9710
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10593
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10340
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10329
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10085
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9163
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6858
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5663
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4304
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.