468,504 Members | 1,966 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,504 developers. It's quick & easy.

fstream buffering


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?

Aug 10 '06 #1
1 3598
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
Aug 11 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by David Briggs | last post: by
7 posts views Thread by Mathias Herrmann | last post: by
9 posts views Thread by Someonekicked | last post: by
2 posts views Thread by Acer | last post: by
1 post views Thread by MForey | last post: by
6 posts views Thread by wiso | last post: by
5 posts views Thread by neowillis | last post: by
6 posts views Thread by Gaijinco | last post: by
1 post views Thread by Sachin Garg | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.