简体   繁体   English

如何在运行程序时捕获输入和输出到文件?

[英]How can i capture the input and output to a file when running a program?

I am working in c++, and I need some way to capture the input and output of the console as if I were working straight through the terminal. 我正在用c ++工作,我需要一些方法来捕获控制台的输入和输出,就像我直接通过终端一样。

so let's say i have an executable test.exe and an input file input.txt and I want to save a combination of the input and output to console.out what terminal command do I need to do? 所以,假设我有一个可执行的test.exe和一个输入文件input.txt,我想将输入和输出的组合保存到console.out我需要做什么终端命令?

I am not great in linux commands, even though I do my best to google, so If you know your help would be much appreciated! 我在linux命令方面不是很好,即使我尽力去谷歌,所以如果你知道你的帮助将不胜感激!

for example, If the input file has in it: 例如,如果输入文件中包含:

show
ignore
hide

and, after running my progam with this input, the output file has in it : 并且,在使用此输入运行我的程序后,输出文件包含在其中:

Enter Command: 
/****SHOWING DATA!****/
Enter Command:
/****IGNORING DATA!***/
Enter Command:
/***HIDING DATA!***/

I want a file that looks like this: 我想要一个看起来像这样的文件:

Enter Command: show
/****SHOWING DATA!****/
Enter Command:ignore
/****IGNORING DATA!***/
Enter Command:hide
/***HIDING DATA!***/

That thus captures what I would see in the terminal if I were to run it without any redirection. 因此,如果我在没有任何重定向的情况下运行它,那么它将捕获我在终端中看到的内容。

The script command might meet your needs. script命令可能满足您的需求。 It will record input and output as it happens and save it to a file. 它将记录输入和输出,并将其保存到文件中。 The only problem is that it has a habit of recording command line or input editing as well so you may have to clean up the output a little. 唯一的问题是它有记录命令行或输入编辑的习惯,所以你可能需要稍微清理一下输出。 Here is a link that explains it a little. 这是一个解释它的链接

You start the utility by simply typing script at the command line. 只需在命令行键入script即可启动该实用程序。 Then you would run your program. 然后你会运行你的程序。 Type Control+D (^D) to tell the script utility to stop recording. 键入Control + D(^ D)告诉script实用程序停止录制。 The output will be in a file named typescript.out in the directory that you started in. 输出将位于您启动的目录中名为typescript.out的文件中。

Updated after Question Change 问题变更后更新

Based on the new question, I don't think that you can easily accomplish what you are asking. 基于这个新问题,我认为你不能轻易完成你的要求。 Only the program knows how much input from stdin that it consumed. 只有程序知道它消耗的stdin量。 There is no good way to know that the program read standard input until the newline was encountered before presenting the next prompt. 在呈现下一个提示之前,没有好的方法可以知道程序读取标准输入直到遇到换行符。

You might try redirection > console.out to capture output. 您可以尝试重定向> console.out来捕获输出。 The script command will capture the entire session. script命令将捕获整个会话。

Have your program, in addition to outputting to the screen, also output to the file. 让您的程序除了输出到屏幕外,还输出到文件中。 Since you are parsing user-inputted commands, you should also have them accessible from variables, just output those too. 由于您正在解析用户输入的命令,您还应该从变量中访问它们,也可以输出它们。

//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.

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