简体   繁体   English

将终端中的标准输入和标准输出重定向到测试程序

[英]Redirect stdin and stdout in terminal to test program

I have a program that reads from stdin and outputs to stdout.我有一个从标准输入读取并输出到标准输出的程序。 I would like to be able to test this via the terminal.我希望能够通过终端进行测试。

I know that我知道

./program < input_file       # Redirects stdin

./program > output_file      # Redirects stdout

output_file < ./program < input_file # Is this supposed to redirect stdin and stdout at the same time?

What if I have sample output in test.out that I want to compare output_file with?如果我在test.out中有要比较output_file的样本 output 怎么办? How can I do this in one go?我怎样才能在一个 go 中做到这一点?

I have tried various ways of doing this, but no luck so far.我尝试了各种方法来做到这一点,但到目前为止还没有运气。

You have the redirection to the output file wrong.您对 output 文件的重定向错误。 You need to use > , not < , and it has to go before the filename.您需要使用> ,而不是< ,并且它必须在文件名之前使用 go 。 Just like in your second example -- it's no different when you're redirecting both input and output.就像在您的第二个示例中一样 - 当您同时重定向输入和 output 时没有什么不同。

./program < input_file > output_file

What you wrote says to run the program named output_file , and redirect its input first from ./program and then from input_file .您写的内容是运行名为output_file的程序,并首先从./program重定向其输入,然后从input_file重定向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM