How to append content to stringstream type object?
Asked Answered
F

2

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

Frogman answered 9/1, 2012 at 9:24 Comment(3)
Try including <sstream> header.Banzai
did you try #include <sstream>?Carpospore
always post compilable code. sting is not valid in this context.Dacoity
B
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.

Banuelos answered 9/1, 2012 at 9:27 Comment(2)
Thanks Vlad very much.... i got resolved error as well as got the correct contain.Frogman
Excellent answer! Just what I needed. See also: en.cppreference.com/w/cpp/io/basic_stringstream/… and en.cppreference.com/w/cpp/io/ios_base/openmodeLorusso
T
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.]

Topple answered 23/10, 2013 at 18:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.