Prompting using stdio

H. S. Teoh via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Sep 28 07:45:43 PDT 2014


On Sun, Sep 28, 2014 at 02:16:29PM +0000, "Nordlöw" via Digitalmars-d-learn wrote:
> I thought
> 
> int main(string[] args)
> {
>     import std.stdio;
>     write(`Press enter to continue: `);
>     stdout.flush;
>     auto line = readln();
>     writeln("Read ", line);
>     return 0;
> }
> 
> would function as a good prompting but it doesn't.
> 
> I outputs the string given to write *after* I've pressed return. Why?

It's an OS limitation. If you're on Posix, you need to switch your
terminal to cbreak mode, otherwise the program doesn't actually receive
any data until after you press Return. I'm not sure what the Windows
equivalent is, but a similar thing happens there -- the input line is
buffered by the OS and the program doesn't see it until Return is
pressed.

You might be interested in Adam Ruppe's terminal.d library, that handles
such low-level details for you:

	https://github.com/adamdruppe/arsd/blob/master/terminal.d


T

-- 
People who are more than casually interested in computers should have at least some idea of what the underlying hardware is like. Otherwise the programs they write will be pretty weird. -- D. Knuth


More information about the Digitalmars-d-learn mailing list