ostringstream and ostream

Started by
14 comments, last by Fruny 19 years, 8 months ago
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?
It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower.Quote:Original post by Toolmaker Quote:Original post by The C modest godHow is my improoved signature?It sucks, just like you.
Advertisement
What header does ostringstream belongs to?
It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower.Quote:Original post by Toolmaker Quote:Original post by The C modest godHow is my improoved signature?It sucks, just like you.
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();

"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
#include <sstream>

I believe the <strstream> file is a pre-standard version, and therefore shouldn't be used.
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
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
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.

#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();
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
I don't know what you're trying to do but here is one version:

#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.
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
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.
How do I write line down into a File?
I tried both File<<"\n" and File<<endl, but they wont go a line down.
It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower.Quote:Original post by Toolmaker Quote:Original post by The C modest godHow is my improoved signature?It sucks, just like you.

This topic is closed to new replies.

Advertisement