I have console program(.exe) which is basically infinite loop:
To be specific, console program code looks like this:
1) read integers n, m
2) n times do:
1. reak integer k
2. read k integers
3) m times do:
1. read integer k
2. read k integers
4)do whatever
5)cout n x m characters which are in ('?', '.', 'X')
6) go to (1)
Here's what it looks like when I run it by hand:
I want to make python program interact with it, but it doesn't work and makes really wierd things.
Here's what I try to even make 1 input and take 1 output.
import subprocess
process = subprocess.Popen("Program.exe", encoding='utf-8', stdin=subprocess.PIPE)
with open('test.txt', 'r') as file:
test = file.read()
test = test + '\n'
print(solver.communicate(input=test))
solver.kill()
Here's the result (output is normal until it start that weird infinite loop):
size:
20 rows. First quantity, then quantity of numbers:
20 columns. First quantity, then quantity of numbers:
.....XXXXX..........
.......XXXX.........
...XXX...XXX........
.XXXXXXX..XX........
.XXXXXXXX.XX........
XX.....XXX.XXXXX....
........XXXXXXXXXX..
....XXXXXXXXX.XXXXX.
...XXXXXXXXXXX..XXX.
..XXX...XXX.XXX..XXX
..XX...XXXXX.XXX..XX
..XX...XX..XXXXX..X.
...X..XX...XX.XXX...
......XX..XXX.XXX...
.....XXX..XX...XX...
.....XX..XX...XX....
.....XX.......X.....
....XXX.............
....XXX.............
....XXX.............
size:
20 rows. First quantity, then quantity of numbers:
20 columns. First quantity, then quantity of numbers:
????????????????????
????????????????????
????????????????????
????????????????????
????????????????????
????????????????????
????????????????????
????????????????????
????????????????????
????????????????????
????????????????????
????????????????????
????????????????????
????????????????????
????????????????????
????????????????????
????????????????????
????????????????????
????????????????????
????????????????????
size:
20 rows. First quantity, then quantity of numbers:
20 columns. First quantity, then quantity of numbers:
????????????????????
.
and it repeats infinitely
.
The question marks mean that there's some input and it takes 20 20 from wherever, but I don't understand where it comes from.
What I want is reliable way to send console program a string and read one back.
I removed hint outputs in console program and it became as easy as just not using communicate. Here's what worked out:
import subprocess
process = subprocess.Popen("Program.exe", encoding='utf-8', stdin=subprocess.PIPE, stdout=subprocess.PIPE)
with open('test.txt', 'r') as file:
test = file.read()
test = test + '\n'
process.stdin.write(test)
process.stdin.flush()
ans = ""
for line in process.stdout:
ans = ans + process.stdout.readline()
print(ans)
solver.kill()