Stringstream returning blank value - c++

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.

Related

I want to make string which is influenced by variables changing (c++)

I'm using stringstream in put variables in string like
int c;
stringstream ss;
string st;
ss << "some texts" << c;
st=ss.str();
cout << st;
but when i change the c and call back ss.str() again, ss.str() is saving
ex-int c, not new one.
Is there any functions or way to string is influenced by changing variable here?
Stringstream doesn't bind to the variable, you have 2 options
1) Create your own class that binds to the int (saves a reference or something)
2) clear the stringstream like
ss.str(""); // clear stream
and then write to it again
ss << "some texts" << c; // set the stream again with modified c

C++ Stringstream only picking up first string

I have a text file with a series two strings delimited by a colon on each line.
I'm using getline to grab the entire line then string stream to split the two strings and put them onto a vector. The code works fine on the first pass it grabs the strings perfectly. Then after that on the 2nd pass of the while loop and so forth it doesn't grab the new input. The string stream seems to leave the original first values for some reason.
if (infile.is_open()) {
std::stringstream ss;
std::string current_line;
std::string tempProxy;
std::string tempPort;
while (std::getline(infile, current_line)) {
ss << current_line;
std::getline(ss, tempProxy, ':');
std::getline(ss, tempPort);
std::cout << tempProxy << " and " << tempPort << std::endl;
}
Any idea why it doesn't want to grab the strings from current_line on any pass except the first iteration?
You're reusing ss but not resetting it correctly. When you extract the second word from the first line, the stream is exhausted and put in an 'EOF' state. When streams are in this or any other 'error' state they don't do anything. You have to clear the error before you can continue to use them.
If you were to check for errors returned by operator<< and getline in the loop (or if you were to cause ss to throw exceptions on errors*) you would find they are indicating that they are not successful past the first iteration. It's a good general practice to always check for errors, and especially so when you're debugging.
You can clear the error by changing your loop:
while (std::getline(infile, current_line)) {
ss.clear(); // clears the error, not the contents
ss << current_line;
However doing this means that ss will accumulate all the lines in its internal buffer. The code will produce your expected output unless the file is large and you run out of memory or something like that.
You can see the accumulating internal buffer with the following:
while (std::getline(infile, current_line)) {
ss.clear();
ss << current_line;
std::cout << "ss internal buffer: " << ss.str();
Instead of using the formatted input to add ss you are probably better off using the .str() member to set it, which will replace the previous data instead of adding to it.
while (std::getline(infile, current_line)) {
ss.clear();
ss.str(current_line);
Alternatively you can construct a new stringstream in each iteration of the loop. This does ensure that no error states or data are carried over from previous iterations. It may also be slower, but you'll have to profile that for yourself.
while (std::getline(infile, current_line)) {
std::stringstream ss(current_line);
* Exceptions are nice because you don't need to remember to check them... except in cases like this where they're not enabled by default. Also I've noticed some C++ implementations have bugs in their iostreams exception code because people don't use it much.
I think you're looking for something like:
if (infile.is_open()) {
std::stringstream ss;
std::string current_line;
std::string tempProxy;
std::string tempPort;
while (std::getline(infile, current_line)) {
std::stringstream to_split;
to_split.str(current_line);
std::getline(to_split, tempProxy, ':');
std::getline(to_split, tempPort);
std::cout << tempProxy << " and " << tempPort << std::endl;
}

passing data from byte array(with zeros) to istringstream(stringstream)

why this code
char magicData [] = { 0x00i8, 0xfdi8, 0xffi8, 0xfci8, 0x00i8,
0xf3i8, 0xf4i8, 0xf5i8, 0x00i8};
std::string s;
std::istringstream ss(magicData, sizeof(magicData));
while(std::getline(ss, s))
{
std::cout << s << std::endl;
}
don't produce any output? (using stringstream instead of istringstream doesn't help).
As result i expect 2 line of string (without 0x00 at end).
How to solve it?
std::istringstream doesn't have a constructor that takes an array. You're actually calling the constructor that takes a C-style string and an openmode. All you need for this to work is:
std::istringstream ss(std::string(magic, magic + sizeof(magic)));

Is there any way to use << operator with stringstream and strings with spaces?

I've ran across this problem while coding my little parser and noticed that stringstream seem to not receive any more data after the space character is met.
Basically
std::stringstream stream;
stream << "Test test";
std::string str;
stream >> str;
std::cout << str;
will print "Test" instead of "Test test". Is there any way to avoid this?
Yes, use std::getline:
std::string str;
std::getline(stream, str);
std::cout << str; // "Test test"

Getting the remainder of a stringstream 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.