68

I'm currently using std::ofstream as follows:

std::ofstream outFile;
outFile.open(output_file);

Then I attempt to pass a std::stringstream object to outFile as follows:

GetHolesResults(..., std::ofstream &outFile){
  float x = 1234;
  std::stringstream ss;
  ss << x << std::endl;
  outFile << ss;
}

Now my outFile contains nothing but garbage: "0012E708" repeated all over.

In GetHolesResults I can write

outFile << "Foo" << std:endl; 

and it will output correctly in outFile.

Any suggestion on what I'm doing wrong?

4
  • 1
    I suggest you rephrase the title to something in the line of: 'writting stringstream contents into ofstream' (or ostream for what matters) Commented Nov 27, 2008 at 22:09
  • 1
    Eric, you should mark Johannes' answer as accepted so that I can delete mine (which is clearly worse than his approach) Commented Jul 19, 2013 at 16:30
  • 1
    Ok, done. But back in the days, I used your solution =)
    – Eric
    Commented Jul 19, 2013 at 16:32
  • :) Someone upvoted my answer today, which might indicate that they chose a less-than-perfect solution when a better one was available. Commented Jul 19, 2013 at 16:39

4 Answers 4

119

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();
3
  • 16
    I'm curious: why this solution works with std::stringstream but not with std::ostringstream? In the second case I am getting an empty file.
    – Javi
    Commented Feb 2, 2015 at 11:29
  • 13
    @JaviV because you can only write to std::oANYSTREAM and only read from std::iANYSTREAM. There will be no sense for output_stream if it would have also read operations. If you need both just use std::ANYSTREAM and you are home :)
    – S.R
    Commented Oct 25, 2017 at 8:11
  • I'm backing here after upvoting years ago. Its a bit weird that the compiler does not warning about call rdbuf from ostringstream Commented Oct 4, 2022 at 4:42
27

If you are using std::ostringstream and wondering why nothing get written with ss.rdbuf() then use .str() function.

outFile << oStream.str();
2
  • 11
    your answer led me to the following realization: to get a char array from ss, you can do ss.str().c_str(). (just posting here for the benefit of newbies who stumble on this in the future.)
    – abcd
    Commented Oct 16, 2015 at 16:57
  • 2
    @Digital_Reality, thanks! nothing was getting written when i used rdbuff. What is the reason for this?
    – Nasir
    Commented Oct 28, 2015 at 6:49
4

When passing a stringstream rdbuf to a stream newlines are not translated. The input text can contain \n so find replace won't work. The old code wrote to an fstream and switching it to a stringstream losses the endl translation.

1
  • 7
    This should be a comment not an answer because you haven't told how to write a std::stringstream to a std::ofstream
    – Alex Bitek
    Commented Jun 12, 2013 at 6:13
2

I'd rather write ss.str(); instead of ss.rdbuf(); (and use a stringstream).

If you use ss.rdbuf() the format-flags of outFile will be reset rendering your code non-reusable. I.e., the caller of GetHolesResults(..., std::ofstream &outFile) might want to write something like this to display the result in a table:

outFile << std::setw(12) << GetHolesResults ...

...and wonder why the width is ignored.

Not the answer you're looking for? Browse other questions tagged or ask your own question.