www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Stdio.write/writeln and flushing

reply Somebody <ajieskola gmail.com> writes:
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?
Sep 08 2016
next sibling parent reply rikki cattermole <rikki cattermole.co.nz> writes:
On 08/09/2016 8:53 PM, 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?
Same problem with Poderosa (terminal emulator which supports Cygwin) its at fault of the software which is client of the pipe. Phobos is doing what it should be doing.
Sep 08 2016
parent Somebody <ajieskola gmail.com> writes:
On Thursday, 8 September 2016 at 08:56:43 UTC, rikki cattermole 
wrote:
 Same problem with Poderosa (terminal emulator which supports 
 Cygwin) its at fault of the software which is client of the 
 pipe. Phobos is doing what it should be doing.
I think you're right... I think half of the command line applications wouldn't run anyway with such a shell regardless what Phobos does. Probably there is a way to configure Emacs otherwise, gotta search out...
Sep 08 2016
prev sibling parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
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 parameter of _IOLBF. -Steve
Sep 08 2016
parent Somebody <ajieskola gmail.com> writes:
On Thursday, 8 September 2016 at 13:22:10 UTC, Steven 
Schveighoffer wrote:
 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 

 mode parameter of _IOLBF.

 -Steve
Ah, good to know an alternative way, I thought just about redefining write(...) and writeln(...) locally. Thanks. I guess the real answer still is to configure OS/Emacs, as apparent when I did dub.init with Emacs.
Sep 09 2016