stdin/stdout and flush
Andre Pany
andre at s-e-a-p.de
Tue Aug 27 20:09:34 UTC 2019
Hi,
I have a small application like this:
---a.d
import std;
void main()
{
while(true)
{
string enemy1 = readln.strip;
int dist1 = to!int(readln.strip);
string enemy2 = readln.strip;
int dist2 = to!int(readln.strip);
writeln((dist1 < dist2) ? enemy1 : enemy2);
stdout.flush();
}
}
---
This applications will be called by a second application:
---b.d
import std;
void main()
{
auto p = pipeShell("a", Redirect.all);
p.stdin.writeln("e1");
p.stdin.writeln("10");
p.stdin.writeln("e2");
p.stdin.writeln("9");
p.stdin.flush();
p.stdout.readln();
import core.thread: Thread;
import core.time: seconds;
Thread.sleep(3.seconds);
}
---
I compile a.d using command ```dmd a.d``` and run b.d using
command ```dmd -run b.d```
In application a.d the first "while round" will succeed as
expected. But in the second "while round" the readln does not
wait until there is an input but gets an empty string. The to!int
will therefore throw an exception.
Why does the readln returns an empty string in the second run and
does not wait on an input?
Kind regards
André
More information about the Digitalmars-d-learn
mailing list