Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old August 10th, 2006, 07:35 PM
ma740988
Guest
 
Posts: n/a
Default 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?

  #2  
Old August 11th, 2006, 01:55 AM
Kai-Uwe Bux
Guest
 
Posts: n/a
Default Re: fstream buffering

ma740988 wrote:
Quote:
>
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
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles