有没有办法将输入和输出重定向到同一个文件?
我有一个 C++ 程序,它输出提示并通过标准输入流 cin 获取用户输入。
我想获得完整的记录,包括程序的输出和文件中的输入。
我知道我可以使用命令行重定向来重定向输入/输出(即 ./program < in.txt > out.txt),但这只会用程序的输出填充 out.txt 以响应来自 in 的输入。 TXT。
我想要一份显示输入和输出的文字记录。也就是说,假设我的程序输出提示“\n输入数字:”,接受用户输入的数字并输出其双倍,“\n你的数字是:”的两倍,并继续执行此操作,直到用户输入 0。
假设我的 in.txt 包含:
1
3
0
然后我想要一份输入/输出的记录:
输入数字:1
您的数字是:2
的两倍 输入数字:3
您的数字是:6
的两倍 输入数字:0
您的数字的两倍是:0
抱歉,如果我没有很好地解释这一点...我真的不知道如何措辞。
有没有办法简单地做到这一点,或者我只需要手动输入输入...并保存终端...
I have a C++ program that outputs prompts and takes user input via the standard input stream cin.
I want to get a full transcript including both the program's output and the input in a file.
I know I can redirect input/output with command-line redirection (i.e. ./program < in.txt > out.txt), but this will only fill out.txt with the program's output in response to the input from in.txt.
I want to have a transcript that shows both the input and output. That is, let's say my program outputs a prompt "\nEnter a number: ", takes a user inputted number and outputs its double, "\nTwice your number is: ", and keeps doing this until the user enters a 0.
Let's say I have in.txt containing:
1
3
0
Then I want to have a transcript of input/output:
Enter a number: 1
Twice your number is: 2
Enter a number: 3
Twice your number is: 6
Enter a number: 0
Twice your number is: 0
Sorry if I didn't explain this very well... I didn't really know how to word it.
Is there a way to do this simply, or do I just have to enter the input by hand... and do some save of the terminal...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
script
不涵盖您的确切用例。您希望看到程序的输入和输出与用户看到的完全一样,但不必自己动手。我找到了 Expect,这似乎正是我们正在寻找的。我不知道 Tcl,但有一个 Python 端口,
pexpect
。您需要安装 pexpect:然后将此代码复制到可执行文件中:
然后您可以像这样运行它:
我的示例运行给了我这个输出:
假设我正确阅读了您的问题,那应该是您的确切答案寻找。它适用于任何程序,无需任何修改。
希望有帮助!
-杰克
script
doesn't cover your exact use case. You'd like to see the input and output to your program exactly as a user would see it, but without having to do it yourself.I found Expect, which seems to be exactly what we're looking for. I don't know Tcl, but there's a Python port,
pexpect
. You'll need to install pexpect:Then copy this code into an executable file:
And then you can run it like so:
My sample run gave me this output:
Assuming I read your question right, that should be the exact answer you're looking for. And it works on any program without any modification at all.
Hope that helps!
-Jake
UNIX
script
命令可以做到这一点。The UNIX
script
command will do it.有趣的问题。应该是跨平台的东西就像下面的例子(我已经在 Windows 和 *nix 上进行了测试,但不幸的是没有 Mac 可以测试)。基本上,您将读取初始文件并提取其数据(在示例中,它假设该文件的格式与您上面提到的完全相同)并将该数据存储在某处。然后,将数据写回您从中读取数据的文件。
我的原始输入文件是:
输出是:
希望这有帮助!
祝你好运!
丹尼斯·M.
Interesting question. Something which should be cross platform is like the example below (I have tested on Windows and *nix but do not have a mac to test on unfortunately). Basically, you will read the initial file and extract its data (in the case of the example, it assumes that the file is formatted exactly as you have mentioned above) and store that data somewhere. Then, write the data back to the file from which you read it.
My original input file was:
The output was:
Hope this helps!
Good luck!
Dennis M.