Writing stringstream contents into ofstream
You can do this, which doesn’t need to create the string. It makes the output stream read out the contents of the stream on the right side (usable with any streams). outFile << ss.rdbuf();
You can do this, which doesn’t need to create the string. It makes the output stream read out the contents of the stream on the right side (usable with any streams). outFile << ss.rdbuf();
You can use the standard manipulators from <iomanip> but there isn’t a neat one that does both fill and width at once: stream << std::setfill(‘0′) << std::setw(2) << value; It wouldn’t be hard to write your own object that when inserted into the stream performed both functions: stream << myfillandw( ‘0’, 2 ) << value; … Read more
The strstream returned a char * that was very difficult to manage, as nowhere was it stated how it had been allocated. It was thus impossible to know if you should delete it or call free() on it or do something else entirely. About the only really satisfactory way to deallocate it was to hand … Read more
This is the way I usually do it: ss.str(“”); ss.clear(); // Clear state flags.
You probably have a forward declaration of the class, but haven’t included the header: #include <sstream> //… QString Stats_Manager::convertInt(int num) { std::stringstream ss; // <– also note namespace qualification ss << num; return ss.str(); }
#include <sstream> and use the fully qualified name i.e. std::stringstream ss;
Typically to ‘reset’ a stringstream you need to both reset the underlying sequence to an empty string with str and to clear any fail and eof flags with clear. parser.str( std::string() ); parser.clear(); Typically what happens is that the first >> reaches the end of the string and sets the eof bit, although it successfully … Read more
stringstream.str() returns a temporary string object that’s destroyed at the end of the full expression. If you get a pointer to a C string from that (stringstream.str().c_str()), it will point to a string which is deleted where the statement ends. That’s why your code prints garbage. You could copy that temporary string object to some … Read more
yourStringStream.str()
#include <iostream> #include <sstream> std::string input = “abc,def,ghi”; std::istringstream ss(input); std::string token; while(std::getline(ss, token, ‘,’)) { std::cout << token << ‘\n’; } abc def ghi