www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - Problems with writef

reply Nick <Nick_member pathlink.com> writes:
I've been having some problems with getting writef() to work properly. The
following example illustrates the problem:

writef("Computing:");
// Some time consuming calculation
for(int i; i<5000; i++)
for(int j; j<10000; j++)
floor(1.23);
writefln(" done.");

This seems to do the entire calculation before writing anything to screen. In
other words, writef doesn't actually write anything to screen until writefln is
called. (I'm on linux, btw.)

This is especially a problem with prompts:

// Btw this line should have been unnecessary :)
alias std.stream.stdin stdin;

writefln("On the next line you are asked to type your name");
writef("Type your name here: ");
char[] name = stdin.readLine();
writefln("Hello ", name, ", that is a nice name!");

The output I get is:

On the next line you are asked to type your name
Nick
Type your name here: Hello Nick, that is a nice name!

I have no idea what the cause of this problem is.

Nick
Aug 12 2004
next sibling parent reply h3r3tic <h3r3tic dev.null> writes:
Nick wrote:
 I've been having some problems with getting writef() to work properly. The
 following example illustrates the problem:
 
 writef("Computing:");
 // Some time consuming calculation
 for(int i; i<5000; i++)
 for(int j; j<10000; j++)
 floor(1.23);
 writefln(" done.");
 
 This seems to do the entire calculation before writing anything to screen. In
 other words, writef doesn't actually write anything to screen until writefln is
 called. (I'm on linux, btw.)
the same on winxp, it doesnt flush, thats the problem. atm the best solution i can come up with is probably calling fflush(std.stdio.stdout); just after writef("Computing:");
Aug 12 2004
parent reply Lars Ivar Igesund <larsivar igesund.net> writes:
This is apparantly neither a D nor a writef problem, but a 'feature' of 
some (most?) consoles. It's the consoles that don't flush until they 
stumble upon a newline. Either add \n to the output of your writef or 
change it to a writefln. I remember this happening when using printf in 
C a long time ago.

Lars Ivar Igesund

h3r3tic wrote:

 Nick wrote:
 
 I've been having some problems with getting writef() to work properly. 
 The
 following example illustrates the problem:

 writef("Computing:");
 // Some time consuming calculation
 for(int i; i<5000; i++)
 for(int j; j<10000; j++)
 floor(1.23);
 writefln(" done.");

 This seems to do the entire calculation before writing anything to 
 screen. In
 other words, writef doesn't actually write anything to screen until 
 writefln is
 called. (I'm on linux, btw.)
the same on winxp, it doesnt flush, thats the problem. atm the best solution i can come up with is probably calling fflush(std.stdio.stdout); just after writef("Computing:");
Aug 12 2004
parent reply h3r3tic <h3r3tic dev.null> writes:
Lars Ivar Igesund wrote:
 This is apparantly neither a D nor a writef problem, but a 'feature' of 
 some (most?) consoles. It's the consoles that don't flush until they 
 stumble upon a newline. Either add \n to the output of your writef or 
 change it to a writefln. I remember this happening when using printf in 
 C a long time ago.
Mostly, yip... however there should be a nicer function than calling file flush on a C output buffer :] and input should be synchronized with output so that when one wants to read from the keybrd, everything is flushed to the console... maybe also adding a special flush object to the stream could be nice... like writef("foobar", flush); and the toStream of instance flush would flush the buffers :>
Aug 13 2004
parent reply Dawid =?UTF-8?B?Q2nEmcW8YXJraWV3aWN6?= <arael fov.pl> writes:
h3r3tic wrote:
 ...
 file flush on a C output buffer :] and input should be synchronized with
 output so that when one wants to read from the keybrd, everything is
 flushed to the console... maybe also adding a special flush object to
 ...
Something like "tie()" in C++ streams. -- Dawid Ciężarkiewicz | arael jid: arael fov.pl
Aug 13 2004
parent Nick <Nick_member pathlink.com> writes:
In article <cfimuh$oak$1 digitaldaemon.com>, Dawid
=?UTF-8?B?Q2nEmcW8YXJraWV3aWN6?= says...
h3r3tic wrote:
 ...
 file flush on a C output buffer :] and input should be synchronized with
 output so that when one wants to read from the keybrd, everything is
 flushed to the console... maybe also adding a special flush object to
 ...
Something like "tie()" in C++ streams.
The simplest solution would be to add a flushWhatever() call to the end of writef's definition, would it not? Buffering screen output can't be much of a time saver anyway. (Well unless you are writing megabytes of output and redirecting to file/pipe, and I guess that is done sometimes. Hmm.) Nick
Aug 13 2004
prev sibling parent Nick <Nick_member pathlink.com> writes:
Another problem is that std.stream.stdout and writef(ln) use different paths for
outputing to the same device, which is can cause problems.

std.stream.stdout actually does the job correctly (flushes even without the
newline), but independently of writef.

writef("snap ");
std.stream.stdout.writeString("crackle ");
writef("pop ");

Output is: "crackle snap pop ". Inserting a printf() into the mess is left as an
exercise for the reader ;-) Perhaps it would be possible to rewrite writef/ln to
just use std.stream.stdout? I.e.

// Pseudo-code
writef(...) { stdout.writeString(format(...)); }
writefln(...) { stdout.writeLine(format(...)); }

Nick
Aug 13 2004