Sign in to follow this  

ostringstream and ostream

This topic is 4859 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

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
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
My code is something like this:

void
LogFile::WriteLine (string Line)
{
if (this->bWriteEnable)
{
ofstream File(this->FileName.c_str(), ios::binary | ios::app);

File<<this->LineNumber<<": "<<Line<<endl;
this->LineNumber++;

File.close();
}
}

LogFile LF("log.fl");

LF.WriteLine ("LineA");
LF.WriteLine ("LineB");

However I get LineALineB instead of:
LineA
LineB

I also tried to put the endl at the begining like this:
File<<endl<<this->LineNumber<<": "<<Line;

Doesnt work either.

Share this post


Link to post
Share on other sites
Why are you opening the file in binary mode?
On MS platforms, an end-of-line is marked by \r\n.

Opening in text mode enables the translation file(\r\n)<->memory(\n), while opening in binary mode disables it.

Unless you really are manipulating binary data (which must not be translated), you have little reason to open the file with an std::ios::binary flag.

Share this post


Link to post
Share on other sites
Quote:
Original post by The C modest god
Ok, the problem is when I open the file with notepad it doesnt do the line downs.
However, with word pad I do see the lines one under the other.


That's because wordpad is smarter than notepad, and is able to deal with UNIX end-of-line conventions (i.e. just a '\n' in the file).

Share this post


Link to post
Share on other sites

This topic is 4859 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this