【发布时间】:2016-06-21 13:20:23
【问题描述】:
“Answering a cli prompt in ruby with open3?”是一个可能重复的问题,但它没有答案。
我需要编写一个程序来编译和执行 C 程序,提供输入并返回输出。到目前为止,我想出了这个:
单输入:
Open3.popen3('one.exe') do |stdin, stdout, stderr|
stdin.puts "45\n"
STDOUT.puts stdout.gets
end
输出是:
Enter the temperature in degrees fahrenheit: The converted temperature is 7.222222
对于两个或多个输入:
Open3.popen3('two.exe') do |stdin, stdout, stderr|
stdin.puts "45 45"
# This line works the same as the previous one.
# stdin.puts "45\r\n45"
stdin.close
STDOUT.puts stdout.gets
end
输出是:
Enter first number: Enter second number: Sum is 90
问题是,我没有取回我输入的内容。
有没有办法可以纠正它,或者可能有更好的方法来做到这一点?
【问题讨论】: