Getting the remainder of a stringstream c++ - c++

I have a stringstream where I need to get the first part out and then get the remainder into a separate string. For example I have the string "This is a car" and I need to end up with 2 strings: a = "This" and b = "is a car".
When I use stringstream to get the first part using <<,then I use the .str() to convert to a string which of course gave me the whole thing "This is a car". How can I get it to play how I want?

string str = "this is a car";
std::stringstream ss;
ss << str;
string a,b;
ss >> a;
getline(ss, b);
EDIT: correction thanks to #Cubbi:
ss >> a >> ws;
EDIT:
This solution can handle newlines in some cases (such as my test cases) but fails in others (such as #rubenvb's example), and I haven't found a clean way to fix it. I think #tacp's solution is better, more robust, and should be accepted.

You can do this: first get the whole string, then get the first word, using substr to get the rest.
stringstream s("This is a car");
string s1 = s.str();
string first;
string second;
s >> first;
second = s1.substr(first.length());
cout << "first part: " << first <<"\ second part: " << second <<endl;
Testing this in gcc 4.5.3 outputs:
first part: This
second part: is a car

You can do a getline on the stream after reading out the first bit....

Another way to do this is with rdbuf:
stringstream s("This is a car");
string first;
stringstream second;
s >> first;
second << s.rdbuf();
cout << "first part: " << first << " second part: " << second.str() << endl;
This may be a good option if you're ultimately going to output the result to a stream instead of a string.

Related

Stringstream returning blank value

Why does this program print out blank?
string str;
stringstream ss(str);
ss << "A string";
cout << str; // just blank
stringstream is not going to modify the argument you pass to its constructor.
Instead, you can get the current buffer from the stringstream by calling its str member function:
cout << ss.str();
Next time, consider reading the documentation.

C++ file input with mixed delimiters and data types

I am trying to input data from a text file:
The line format is as follows...
String|String|int double
Example:
Bob|oranges|10 .89
I can get the line in as a string using
Getline(infile, line)
I don't understand how to break the line into the distinct variables from the string variable.
Thanks
for a start you could write some good old fashioned c code using strchr.
Or use string.find / find_first_of if you are using std::String
http://www.cplusplus.com/reference/string/string/find_first_of/
You marked this as C++. So perhaps you should try to use formatted extractors ...
Here is a 'ram' file (works just like a disk file)
std::stringstream ss("Bob|oranges|10 .89");
// this ^^^^^^^^^^^^^^^^^^ puts one line in file
I would use getline for the two strings, with bar terminator
do {
std::string cust;
(void)std::getline(ss, cust, '|'); // read to 1st bar
std::string fruit;
(void)std::getline(ss, fruit, '|'); // read to 2nd bar
Then read the int and float directly:
int count = 0;
float cost;
ss >> count >> cost; // the space char is ignored by formatted extraction
std::cout << "\ncust: " << cust << "\n"
<< " " << count << " " << fruit
<< " at $" << cost
<< " Totals: " << (float(count) * cost) << std::endl;
if(ss.eof()) break;
}while(0);
If you are to handle more lines, you need to find the eoln, and repeat for every record of the above style.
This approach is extremely fragile (any change in format will force a change in your code).
This is just to get your started. It has been my experience that using std::string find and rfind is much less fragile.
Good luck.

White space insertion in std::stringstream fails

I'm trying to insert white space in std::stringstream in this way.
std::stringstream sstr;
sstr.str("");
sstr << " ";
sstr << 10;
and then setting it as a label like that
label->setString(sstr.str().c_str());
but it's only giving me 10, space is not included. I've followed many links to solve problem but of no use. Following link suggests to use getline() but in my case I cannot do that :
stringstream doesn't accept white space?
I've also tried to use std::noskipws but it also not work :
sstr << std::noskipws << " ";
Any help will be appreciated.
std::stringstream should not remove your whitespace when used like this. Are you sure that it is not the label-object that is trimming your string and removing the whitespace?
Try debugging or printing out your string before setting it to the label.
You can use put method to append a single char:
std::stringstream sstr;
sstr.str("");
sstr.put(' ');
sstr.put(10);
I think you're using it wrong:
string labelstr;
std::stringstream sstr;
sstr.str("");
sstr << " ";
sstr << 10;
ss >> noskipws >> labelstr;
label->setString(labelstr);
http://www.cplusplus.com/reference/ios/noskipws/
Your code works as expected for me. Here's a runnable example: http://cpp.sh/8d6.
The culprit must be setString trimming your input. Or possibly some other function that reads the string later.

std::stringstream only supports one input at a time?

Since std::stringstream is a stzream, and according to the documention here, you can perform any operation a stream supports.
So I expected the following sample to work, but it seems it doesn't. I'm using MingW with gcc 4.8.3.
Variant A:
std::string s;
std::stringstream doc;
doc << "Test " << "String ";
doc << "AnotherString";
doc >> s;
std::cout << s << std::endl;
Variant B:
std::string s;
std::stringstream doc;
doc << "Test ";
doc << "AnotherString";
doc >> s;
std::cout << s << std::endl;
The output of this is only
Test
While I expected that it would concatenate the individual strings until I read from the stream back what I put there.
So what is the approperiate way to concatenate strings? Do I really have to read out each one individually and concatenate them manually, which seems quite awkward to me in C++.
It is putting each of the strings into doc, so that its content is:
Test String AnotherString
Then when you extract using doc >> s, it only reads up to the first whitespace. If you want to get the entire stream as a string, you can call str:
std::cout << doc.str() << std::endl;
It will only read one word till a white-space by using stream >> s. Besides #JosephMansfield's answer of using str(), alternatively you can use getline() (works perfectly if you the string doesn't contains new lines):
getline(doc, s);

how to get string from a line of strings?

I have following string:
"hw_core_detectionhook::Iocard const*"
I have to get only first part, i.e all text present before space, i.e I need the "hw_core_detectionhook::Iocard" part only.
std::stringstream ss;
ss << "hw_core_detectionhook::Iocard const*";
std::string s;
ss >> s;
std::cout << s;
Output:
hw_core_detectionhook::Iocard
See the complete demo online : http://www.ideone.com/w9l1C
s.substr(0,s.find_first_of(" "));