digitalmars.D.learn - stdin/stdout and flush
- Andre Pany (44/44) Aug 27 2019 Hi,
- a11e99z (3/21) Aug 27 2019 probably cuz no second "send round" in b.d.
- Andre Pany (11/35) Aug 27 2019 I mean the coding in the while loop is executed twice.
- a11e99z (9/11) Aug 27 2019 what result u expecting?
- a11e99z (3/8) Aug 27 2019 its not sent, "b" just read from pipe
- a11e99z (3/6) Aug 27 2019 "b" should send data in loop too, then "a" will work ok.
- Andre Pany (6/12) Aug 27 2019 Thanks for the answers. I currently try to make the Codingame
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é
Aug 27 2019
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"?
Aug 27 2019
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: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 AndreThis 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"?
Aug 27 2019
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: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 => errorOn Tuesday, 27 August 2019 at 20:09:34 UTC, Andre Pany wrote:
Aug 27 2019
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:its not sent, "b" just read from pipe pipe closed and null (no data) come to "a"On Tuesday, 27 August 2019 at 20:45:44 UTC, a11e99z wrote:what result u expecting? then u send from "b" p.stdout.readln(); that is read as 2ndOn Tuesday, 27 August 2019 at 20:09:34 UTC, Andre Pany wrote:
Aug 27 2019
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:"b" should send data in loop too, then "a" will work ok. do u still hack CodinGame? :)On Tuesday, 27 August 2019 at 20:09:34 UTC, Andre Pany wrote:
Aug 27 2019
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: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 AndreOn Tuesday, 27 August 2019 at 20:45:44 UTC, a11e99z wrote:"b" should send data in loop too, then "a" will work ok. do u still hack CodinGame? :)On Tuesday, 27 August 2019 at 20:09:34 UTC, Andre Pany wrote:
Aug 27 2019