c++ – How can I copy value from ostringstream to string?

c++ – How can I copy value from ostringstream to string?

string str = oss.str(); // this should do the trick

If youre trying to copy the whole file to a stringstream, then this:

oss << ifs;

is wrong. All that does is prints the address of ifs. What you want to do is this:

oss << ifs.rdbuf();

And then of course, to copy that to a string, like the others are saying:

str = oss.str();

If you just want to get a single line, then skip the stringstream, and just use getline:

std::getline(ifs,str);

c++ – How can I copy value from ostringstream to string?

<< is an operator defined on streams, which a string is not. You just want to use = here.

Leave a Reply

Your email address will not be published. Required fields are marked *