Jump to content
  • Advertisement
Sign in to follow this  
The C modest god

ostringstream and ostream

This topic is 5227 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
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();

Share this post


Link to post
Share on other sites
#include <sstream>

I believe the <strstream> file is a pre-standard version, and therefore shouldn't be used.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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 other
File << 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();


Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!