Stdio.write/writeln and flushing

Steven Schveighoffer via Digitalmars-d digitalmars-d at puremagic.com
Thu Sep 8 06:22:10 PDT 2016


On 9/8/16 4:53 AM, Somebody wrote:
> If i write something like:
>
> writeln("what to do?");
> switch(readln[0 .. $ - 1])
> {   //..
> }
> writeln("bye");
>
> ....that works just as it should at Windows, started from a command
> prompt. However, if I run it from GNU Emacs, I have to manually flush
> the output after each time I do it before taking input, or else the
> output does not show when it should.
>
> I wonder if write(...) and writeln(...) should automatically flush, to
> enhance portability? Of course, I may be issing missing something?

write and writeln depend on the behavior of FILE * for flushing, there 
is no specific flush.

Note that FILE * examines the file descriptor and if it is detected as 
an interactive descriptor, flush is done every newline. If not, then 
flush is only done when the buffer is full. This is standard behavior 
forever, and is done to avoid performance problems when piping the 
result of a command to a file, for instance (flushing is expensive).

Your emacs "console" is not marked by the OS as interactive (or however 
it's detected by FILE *, implementation defined), therefore flush does 
not happen on newlines.

If you want to force newline flushing, use 
http://dlang.org/phobos/std_stdio.html#.File.setvbuf with a mode 
parameter of _IOLBF.

-Steve


More information about the Digitalmars-d mailing list