OutputStreams and buffering

I'd like some information, stored in a buffer, to be written to the file, when I call close() on the file.

I have looked at FileBuf and pubsetbuf, among others. However, after testing, they are not buffering it (the way I expect them to).

How can I achieve the same as BufferedOutputStream(Java) in C++?

fprintf writes are buffered, but the OS may flush the buffer when deemed convenient. Writes to stderr are an exception and are not buffered.

If this bothers you, I suggest creating a secondary buffer, which you then write to the file and fflush().
Thanks for that :)
What exactly is the behaviour you are/are not seeing? Are individual characters being written to the file with every write?
Use a stringstream for buffering:
http://www.cplusplus.com/reference/iostream/stringstream/

1
2
3
4
#include <sstream>
//...
stringstream ss;
ss << "Buffered text";

Then send it to stdout with:
 
cout << ss.str();

Last edited on
Topic archived. No new replies allowed.