“用 open3 回答 ruby 中的 cli 提示? ”可能是重复的问题,但它没有答案。
我需要编写一个程序来编译和执行 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
问题是,我没有取回我输入的输入。
有没有办法可以纠正它,或者可能有更好的方法来做到这一点?