views:

1352

answers:

3

I'm currently using a std::ofstream a function and a std::stringstream

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

Then I call a function

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 the outFile

Any suggestion on what I'm doing wrong?

+7  A: 

You are dumping the stringstream, and not its contents:

outFile << ss.str(); // obtain the contents as an std::string
David Rodríguez - dribeas
A: 

weird thing is that if ofstream is opened in the same function, i dont need the .str()

Eric
+5  A: 

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();
Johannes Schaub - litb