Inconsistent stdout buffering behaviour

Marco Righele marco.righele at gmail.com
Wed Aug 11 06:49:32 PDT 2010


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20100811/bfb30ae4/attachment.html>


More information about the Digitalmars-d mailing list