16

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 " << " Continuing 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

Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265
BSalunke
  • 11,499
  • 8
  • 34
  • 68

2 Answers2

23

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.

Vlad
  • 18,195
  • 4
  • 41
  • 71
  • 1
    Thanks Vlad very much.... i got resolved error as well as got the correct contain. – BSalunke Jan 09 '12 at 09:42
  • 1
    Excellent answer! Just what I needed. See also: https://en.cppreference.com/w/cpp/io/basic_stringstream/basic_stringstream and https://en.cppreference.com/w/cpp/io/ios_base/openmode – Gabriel Staples Sep 03 '22 at 05:51
17

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.]

Anthony Hall
  • 1,442
  • 2
  • 16
  • 22