unbuffered output file

R

Rich

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.
 
D

David Harmon

On 15 Apr 2004 10:24:43 -0700 in comp.lang.c++, (e-mail address removed)
(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::eek:ut);
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.
 
J

John Harrison

David Harmon said:
On 15 Apr 2004 10:24:43 -0700 in comp.lang.c++, (e-mail address removed)
(Rich) wrote,

I am curious as to why you do not want buffering, but,


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


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
 
D

David Harmon

On Thu, 15 Apr 2004 19:36:53 +0100 in comp.lang.c++, "John Harrison"
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::eek:ut|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";
}
 
D

David Harmon

On Fri, 16 Apr 2004 00:34:09 GMT in comp.lang.c++, Kevin Goodsell
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top