By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 777 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

unbuffered output file

P: n/a
Does anyone know the correct way of opening an unbuffered output file
using STL?

I attempted:

ofstream myfile("fname.txt", ios::binary);

myfile.setbuf(NULL, 0);

and I was informed that setbuf is not a member of
basic_ofstream.......

My platform is Win NT and I'm using VC++ 6.0

I have read that setbuf is (now, and seems to have recently become?) a
protected member of this class, and so I've tried a few other methods
of doing it to no avail.

I'm getting frustrated enough to simply do it in 'C'!

R.
Jul 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On 15 Apr 2004 10:24:43 -0700 in comp.lang.c++, ri**********@supanet.com
(Rich) wrote,
Does anyone know the correct way of opening an unbuffered output file
using STL?
I am curious as to why you do not want buffering, but,
ofstream myfile("fname.txt", ios::binary);
Due to some buggered implementations, it may be safer to write
ofstream myfile("fname.txt", ios::binary|ios::out);
myfile.setbuf(NULL, 0);

and I was informed that setbuf is not a member of
basic_ofstream.......


I believe that should be
myfile.rdbuf()->pubsetbuf(0, 0);

Of course, it is important to call this before any i/o has occurred on
the stream.

Jul 22 '05 #2

P: n/a

"David Harmon" <so****@netcom.com> wrote in message
news:40***************@news.west.earthlink.net...
On 15 Apr 2004 10:24:43 -0700 in comp.lang.c++, ri**********@supanet.com
(Rich) wrote,
Does anyone know the correct way of opening an unbuffered output file
using STL?


I am curious as to why you do not want buffering, but,
ofstream myfile("fname.txt", ios::binary);


Due to some buggered implementations, it may be safer to write
ofstream myfile("fname.txt", ios::binary|ios::out);
myfile.setbuf(NULL, 0);

and I was informed that setbuf is not a member of
basic_ofstream.......


I believe that should be
myfile.rdbuf()->pubsetbuf(0, 0);

Of course, it is important to call this before any i/o has occurred on
the stream.


Its also implementation defined whether or not this actually causes the file
to become unbuffered.

Really we are in OS specific territory here. If it really is vital that you
have an unbuffered file then you are probably better leaving standard C++
behind and using your operating system API directly.

john
Jul 22 '05 #3

P: n/a
On Thu, 15 Apr 2004 19:36:53 +0100 in comp.lang.c++, "John Harrison"
<jo*************@hotmail.com> wrote,
I believe that should be
myfile.rdbuf()->pubsetbuf(0, 0);

Of course, it is important to call this before any i/o has occurred on
the stream.


Its also implementation defined whether or not this actually causes the file
to become unbuffered.


As I recall, this is supposed to be the one exact incantation whereby
the standard says the result is unbuffered and output happens "as soon
as possible". I can't actually check right now. I guess "as soon as
possible" is still implementation defined.

Well damn. No discernable difference with MSVC 6.0.
Gigantic difference with Digital Mars C++ http://www.digitalmars.com

Test MSVC6 DM
Default 38-40 s 10-11 s
Little buffer 38-40 s 21-22 s
Unbuffered 38-40 s still waiting for it to finish.
#include <iostream>
#include <fstream>
#include <iterator>
#include <time.h>
using namespace std;

int main(int argc, char **argv)
{
char x[] = {"0123456789"};
char buf[100];

ofstream out("t5.out", ios::out|ios::binary);
if(argc == 3) {
out.rdbuf()->pubsetbuf(NULL, 0);
cerr << "Unbuffered\n";
} else if(argc == 2) {
out.rdbuf()->pubsetbuf(buf, sizeof buf);
cerr << "Little buffer\n";
} else {
cerr << "Default\n";
}

time_t starting_time, ending_time;
time(&starting_time);

for(int i = 0; i < 2000000; ++i)
out.write(x, 10);

time(&ending_time);
int elapsed = difftime(ending_time, starting_time);
cerr << "Elapsed time " << elapsed << " seconds\n";
}
Jul 22 '05 #4

P: n/a
David Harmon wrote:

Well damn. No discernable difference with MSVC 6.0.


This could be because of a bug in the library that causes buffering to
be disabled on any file opened by name.

http://www.dinkumware.com/vc_fixes.html

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Jul 22 '05 #5

P: n/a
On Fri, 16 Apr 2004 00:34:09 GMT in comp.lang.c++, Kevin Goodsell
<us*********************@neverbox.com> wrote,

This could be because of a bug in the library that causes buffering to
be disabled on any file opened by name.

http://www.dinkumware.com/vc_fixes.html


I thought I got them all. That was added since last time I looked.
Hah! Now with the fix in, I get:

Test MSVC6
Default 13-14 sec
Little buffer 26-28 sec
Unbuffered 13-14 sec Explain that?

Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.