Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
August 27, 2019 stdin/stdout and flush | ||||
---|---|---|---|---|
| ||||
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é |
August 27, 2019 Re: stdin/stdout and flush | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andre Pany | On Tuesday, 27 August 2019 at 20:09:34 UTC, Andre Pany wrote:
>
> This applications will be called by a second application:
> 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();
> }
>
> 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?
>
probably cuz no second "send round" in b.d.
or u mean second half-round "e2,9"?
|
August 27, 2019 Re: stdin/stdout and flush | ||||
---|---|---|---|---|
| ||||
Posted in reply to a11e99z | On Tuesday, 27 August 2019 at 20:45:44 UTC, a11e99z wrote:
> On Tuesday, 27 August 2019 at 20:09:34 UTC, Andre Pany wrote:
>>
>> This applications will be called by a second application:
>> 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();
>> }
>>
>> 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?
>>
>
> probably cuz no second "send round" in b.d.
> or u mean second half-round "e2,9"?
I mean the coding in the while loop is executed twice.
If I start application A in the console using ```dmd -run a.d``` the readln functions are waiting on stdin input in first and second execution of the while logic.
But if application A is called from application B then in the second execution of the while loop, the readln statements just return empty strings.
Do I have to clear some buffers?
Kind regards
Andre
|
August 27, 2019 Re: stdin/stdout and flush | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andre Pany | On Tuesday, 27 August 2019 at 21:01:58 UTC, Andre Pany wrote:
> On Tuesday, 27 August 2019 at 20:45:44 UTC, a11e99z wrote:
>> On Tuesday, 27 August 2019 at 20:09:34 UTC, Andre Pany wrote:
what result u expecting?
u send from "b": e1 10 e2 9
"a" read it. 1st loop finished.
then u send from "b" p.stdout.readln(); that is read as 2nd enemy1=null
and finally sleep and exit from b.main where pipe is closed and "a" read null again (no data)
dist1 = null.to!int => error
|
August 27, 2019 Re: stdin/stdout and flush | ||||
---|---|---|---|---|
| ||||
Posted in reply to a11e99z | On Tuesday, 27 August 2019 at 21:28:05 UTC, a11e99z wrote:
> On Tuesday, 27 August 2019 at 21:01:58 UTC, Andre Pany wrote:
>> On Tuesday, 27 August 2019 at 20:45:44 UTC, a11e99z wrote:
>>> On Tuesday, 27 August 2019 at 20:09:34 UTC, Andre Pany wrote:
>
> what result u expecting?
>
> then u send from "b" p.stdout.readln(); that is read as 2nd
its not sent, "b" just read from pipe
pipe closed and null (no data) come to "a"
|
August 27, 2019 Re: stdin/stdout and flush | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andre Pany | On Tuesday, 27 August 2019 at 21:01:58 UTC, Andre Pany wrote:
> On Tuesday, 27 August 2019 at 20:45:44 UTC, a11e99z wrote:
>> On Tuesday, 27 August 2019 at 20:09:34 UTC, Andre Pany wrote:
>>>
"b" should send data in loop too, then "a" will work ok.
do u still hack CodinGame? :)
|
August 28, 2019 Re: stdin/stdout and flush | ||||
---|---|---|---|---|
| ||||
Posted in reply to a11e99z | On Tuesday, 27 August 2019 at 21:42:55 UTC, a11e99z wrote:
> On Tuesday, 27 August 2019 at 21:01:58 UTC, Andre Pany wrote:
>> On Tuesday, 27 August 2019 at 20:45:44 UTC, a11e99z wrote:
>>> On Tuesday, 27 August 2019 at 20:09:34 UTC, Andre Pany wrote:
>>>>
>
> "b" should send data in loop too, then "a" will work ok.
> do u still hack CodinGame? :)
Thanks for the answers. I currently try to make the Codingame puzzles available for D by writing a local application which has similar puzzles and runs the D source code.
Kind regards
Andre
|
Copyright © 1999-2021 by the D Language Foundation