![](/img/trans.png)
[英]How can I clean this program efficiently and still detect when the file input is not a letter?
[英]How can i capture the input and output to a file when running a program?
我正在用c ++工作,我需要一些方法来捕获控制台的输入和输出,就像我直接通过终端一样。
所以,假设我有一个可执行的test.exe和一个输入文件input.txt,我想将输入和输出的组合保存到console.out我需要做什么终端命令?
我在linux命令方面不是很好,即使我尽力去谷歌,所以如果你知道你的帮助将不胜感激!
例如,如果输入文件中包含:
show
ignore
hide
并且,在使用此输入运行我的程序后,输出文件包含在其中:
Enter Command:
/****SHOWING DATA!****/
Enter Command:
/****IGNORING DATA!***/
Enter Command:
/***HIDING DATA!***/
我想要一个看起来像这样的文件:
Enter Command: show
/****SHOWING DATA!****/
Enter Command:ignore
/****IGNORING DATA!***/
Enter Command:hide
/***HIDING DATA!***/
因此,如果我在没有任何重定向的情况下运行它,那么它将捕获我在终端中看到的内容。
您可以尝试重定向> console.out
来捕获输出。 script命令将捕获整个会话。
让您的程序除了输出到屏幕外,还输出到文件中。 由于您正在解析用户输入的命令,您还应该从变量中访问它们,也可以输出它们。
//PSEUDO-CODE!
std::ofstream fout;
fout.open("output.txt");
std::cout << "Enter Command: ";
fout << "Enter Command: ";
std::cin >> cmd;
fout << cmd << '\n';
//..Output results to file.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.