i am using object of stringstream as follows:
#include<iostream>
#include <istream>
using namespace std;
int main()
{
sting str="my.string";
std::stringstream message("HI this is firet line from initialization");
message << " second line " << " Continueing second line";
message << ", Add values: ";
message << str ;
std::cout<<"message value is :"<<message.str()<<std::endl;
return 0;
}
with above code it gives an error as follows:
error: variable 'std::stringstream message' has initializer but incomplete type
above error is resolved once i added the "#include " header file. but when i printed the value of message. it is getting incomplete. i.e. value i got of message is as follows:
message value is : second line Continueing second linetion , Add values: my.string
any suggestion on why first line is removing in output? Thanks in advance
To use stringstream class in the C++ program, we have to use the header <sstream>. For Example, the code to extract an integer from the string would be: string mystr(“2019”); int myInt; stringstream (mystr)>>myInt; Here we declare a string object with value “2019” and an int object “myInt”.
It is commonly used in parsing inputs and converting strings to numbers, and vice-versa. The most commonly used methods and operators from this class are: str(): Gets and sets the string object's content in the stream. clear(): Clears the stream.
A stringstream associates a string object with a stream allowing you to read from the string as if it were a stream (like cin). To use stringstream, we need to include sstream header file. The stringstream class is extremely useful in parsing input.
Very Informally: A string is a collection of characters, a stream is a tool to manipulate moving data around. A string stream is a c++ class that lets you use a string as the source and destination of data for a stream.
stringstream
is defined in a different header:
#include <sstream>
Also, if you want the initial contents to stick you'll want something like:
std::stringstream message("HI this is firet line from initialization",
ios_base::app | ios_base::out);
where app
means that every output operation writes to the end of the stream. See here.
Following Vlad's answer, I just tried the following (in GCC 4.7.0, mingw on Windows 7: x86_64-w64-mingw32)
std::stringstream ss("initial string", ios_base::app | ios_base::out);
ss << " appended string";
But afterward, ss.good()
would return false immediately afterward, which was confusing, since ss.str()
was still returning the appended string I expected, "initial string appended string"
. I finally stumbled upon the following line from http://www.cplusplus.com/reference/sstream/stringstream/stringstream/:
Other values of type ios_base::openmode (such as ios_base::app) may also be specified, although whether they have an effect on stringstream objects depends on the library implementation.
After reading about the "standard" ios_base::
flags, I tried this, which gave me the appended stream contents I expected, with ss.good()
returning true
:
std::stringstream ss("initial string", ios_base::ate | ios_base::in | ios_base::out);
[P.S. I would rather have posted this as a comment on Vlad's answer, since it's not a direct answer; it's only a comment on his suggestion for preserving previous stream contents when using operator <<
. Since I don't have enough reputation points to comment on his answer, but I think this will be useful to others who come across this, I'm posting it as an answer.]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With