Reputation: 357
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.
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?
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!
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.
Upvotes: 1
Views: 2312
Reputation: 10936
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.
Upvotes: 0
Reputation: 59553
The script
command might meet your needs. 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. Then you would run your program. Type Control+D (^D) to tell the script
utility to stop recording. The output will be in a file named typescript.out
in the directory that you started in.
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. There is no good way to know that the program read standard input until the newline was encountered before presenting the next prompt.
Upvotes: 2