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...
On a command line, redirection is the process of using the input/output of a file or command to use it as an input for another file. It is similar but different from pipes, as it allows reading/writing from files instead of only commands. Redirection can be done by using the operators > and >> .
The > symbol is known as the output redirection operator. The output of a process can be redirected to a file by typing the command followed by the output redirection operator and file name.
Input/Output (I/O) redirection in Linux refers to the ability of the Linux operating system that allows us to change the standard input ( stdin ) and standard output ( stdout ) when executing a command on the terminal. By default, the standard input device is your keyboard and the standard output device is your screen.
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:
wget http://pexpect.sourceforge.net/pexpect-2.3.tar.gz
tar xzf pexpect-2.3.tar.gz
cd pexpect-2.3
sudo python ./setup.py install
Then copy this code into an executable file:
#! /usr/bin/env python
import sys, pexpect
executable = sys.argv[1]
infile = sys.argv[2]
proc = pexpect.spawn(executable)
file = open(infile)
for line in file:
proc.send(line)
proc.sendeof()
proc.expect(pexpect.EOF)
print proc.before,
And then you can run it like so:
transcript ./executablefile fileforinput
My sample run gave me this output:
Enter a number: 1
Twice your number is: 2
Enter a number: 2
Twice your number is: 4
Enter a number: 3
Twice your number is: 6
Enter a number: 0
Twice your number is: 0
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
The UNIX script
command will do it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With