Call ofstream::write with a stringstream type

I'm trying to write out a binary file. I have the data correctly inside a stringstream but the ofstream::write function wants a const char*.

[code=C++] std::stringstream str;

BinaryWriter writer(str); // From Poco library. str
writer << true
<< 'T'
<< 42
<< 3.14159265
<< "foo bar";

std::ofstream datFile;
datFile.open("test.dat", std::ofstream::binary);
datFile.write(reinterpret_cast<const char*>(str.str().c_str()), sizeof(str));
datFile.close();
[/code]
The datFile.write line works but it looks very ugly! Also, the sizeof(str) doesn't return the correct length.

I'd sure appreciate any suggestions!
Thanks!
Try this instead of line 12:
datFile.write( str.str().c_str(), str.str().length() );
Thank you!
Last edited on
Topic archived. No new replies allowed.