Thread overview
Problems with writef
Aug 12, 2004
Nick
Aug 12, 2004
h3r3tic
Aug 13, 2004
Lars Ivar Igesund
Aug 13, 2004
h3r3tic
Aug 13, 2004
Nick
Aug 13, 2004
Nick
August 12, 2004
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


August 12, 2004
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:");
August 13, 2004
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:");
August 13, 2004
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 :>
August 13, 2004
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
August 13, 2004
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


August 13, 2004
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