ostringstream and ostream
How do I open an ostream file to append to?
Can I do this?
string str;
ostringstream ostr;
ostream File;
ostr<<str; // Needs to be str.c_str()?
File<<ostr; // Is it ok to do?
You can write a std::string out to an std::ostream just fine, no need for .c_str(). To write a std::ostringstream to a std::ostream, however, use the .str() member function on the std::ostringstream to get a std::string, which will be written to the std::ostream like normal.
string str;ostringstream ostr;ostream File;ostr<<str;File<<ostr.str();
#include <sstream>
I believe the <strstream> file is a pre-standard version, and therefore shouldn't be used.
I believe the <strstream> file is a pre-standard version, and therefore shouldn't be used.
To answer the OP's queston ostream has an operator<< which takes an ostream as an argument, so all ostreams and object's who have ostream as a parent should be acceptable, including ostringstream, and ofstream is an output stream, that interacts with files.Hopefully this is helpful.
EDIT: seems a previous poster is right,sstream for ostringstream
EDIT: seems a previous poster is right,sstream for ostringstream
When you want to copy the content of one stream to another, the best solution is to do something like: dst << src.rdbuf();.
The rdbuf() member function returns the internal stream buffer object, and all ostreams have an overload that supports writing such an object. The operation is lower-level (i.e. faster!) than going through the usual formatted IO operations.
This also works if you want to copy a whole file (add binary flags - std::ios::binary as appropriate).
The rdbuf() member function returns the internal stream buffer object, and all ostreams have an overload that supports writing such an object. The operation is lower-level (i.e. faster!) than going through the usual formatted IO operations.
#include <string>#include <sstream>#include <fstream>std::string str;std::ostringstream ostr;// But why aren't you directly writing the string to the file?ostr << str;// open blah.txt for append (create if doesn't exist)std::ofstream File( "blah.txt", std::ios::app );// Write the contents of one stream to the otherFile << ostr.rdbuf();
This also works if you want to copy a whole file (add binary flags - std::ios::binary as appropriate).
#include <fstream>std::ifstream ifs("input.txt");std::ofstream ofs("output.txt");ofs << ifs.rdbuf();
I don't know what you're trying to do but here is one version:
Otherwise, istringstream and ostringstream resides in the header file: sstream
#include <fstream>const char *filename = "myfile.txt"int main(){ std::string text("some text to be appended"); std::ofstream myfile(filename, ios::app); myfile << text << endl; return 0;}
Otherwise, istringstream and ostringstream resides in the header file: sstream
Dude, I don't know how I missed it. I was looking at all the overloaded << functions and missed this one:
basic_ostream& basic_ostream::operator<<(basic_streambuf<E, T> *sb);
So not surprisingly, Fruny is correct.
Like others have said, stringstreams are for parsing to & from strings in memory, you don't need to do that for file streams as they already format/parse to and from text files.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement