www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Inconsistent stdout buffering behaviour

Greetings,

I noticed a different behaviour when using stdout.writeln() instead of
writeln() in FreeBSD.
It seems that the first issues an explicit fflush each time it's called,
while the latter doesn't.
This makes a difference at least in FreeBSD, where writes to the stdout are
line-buffered
by the system only when the destination is a real console and not, for
example, a file or a pipe.
In that case, one call will write the output one line at time, while the
other won't.

It can be easily seen with the following trivial program, if you pipe it's
output to "cat".
(using dmd 2.0.47 under FreeBSD 8.1)

module hello;

import std.stdio;
import core.sys.posix.unistd;

void main()
{
    foreach(n;0..100_000)
    {
        writeln("Hello World!");
        // stdout.writeln("Hello World!");
        sleep(1);
    }
}


I'm not sure which of the two behaviours is the right one (the C version of
the program using printf behaves like writeln, the C++ version using cout
behaves like stout.writeln...)


bye,
Marco
Aug 11 2010