ma740988 wrote:
>
I'm perusing Langer/Kreft trying to find a solution to an issue here.
Thus far, it's some heavy reading. In any event, given:
int main()
{
std::ofstream ofs ( "my_file.txt" );
if (!ofs )
return EXIT_FAILURE;
for ( int idx ( 0 ); idx < 10 ; ++idx )
{
ofs << idx
}
ofs.close ();
}
I'd like to turn off 'buffering' . How would i achieve that?
try:
#include <fstream>
int main()
{
std::ofstream ofs ( "my_file.txt" );
if (!ofs )
return EXIT_FAILURE;
ofs.rdbuf()->pubsetbuf(0,0);
for ( int idx ( 0 ); idx < 10 ; ++idx )
{
ofs << idx;
}
ofs.close ();
}
From the standard:
basic_streambuf<char_type,traits>* pubsetbuf(char_type* s, streamsize n);
Returns: setbuf(s,n)
For file based stream buffers [27.8.1.4/10]
basic_streambuf* setbuf(char_type* s, streamsize n);
Effects: If setbuf(0,0) is called on a stream before any I/O has occured
on that stream, the stream becomes unbuffered. Otherwise the results are
implementation-defined. "Unbuffered" means that pbase() and pptr() always
return null and output to the file should appear as soon as possible.
Best
Kai-Uwe Bux