How can I copy a value from ostringstream to a string?

I tried:

ostringstream oss;
read a string from file and put to oss;
string str;
str << oss.str();// error here "error: no match for ‘operator>>’ in 'oss >> str' "

If I use str = oss.str(); Instead of printing the value of the string, it prints "....0xbfad75c40xbfad75c40xbf...."like the memory address.
Can someone tell me why? Thank.

+3
source share
4 answers
string str = oss.str(); // this should do the trick
+14
source

If you are trying to copy the entire file to a string stream, then this:

oss << ifs;

wrong. All that does is print the ifs address. What you want to do is:

oss << ifs.rdbuf();

And then, of course, to copy this into a string, as others say:

str = oss.str();

If you just want to get one line, then skip the string and just use getline:

std::getline(ifs,str);
+3
source

<< - , , a string . = .

+1

That doesn't make any sense. oss.str()returns a std::string. You cannot transfer stringto string. You either need to str = oss.str(), or use the standard stringstream, and do it ss >> str.

+1
source

Source: https://habr.com/ru/post/1794151/


All Articles