Is there a way to redirect input and output to the same file?

I have a C ++ program that prints hints and enters user input through a standard input stream.

I want to get a full decryption, including program output and file entry.

I know that I can redirect I / O with command line redirection (i.e. ../ program <in.txt> out.txt), but this will only populate out.txt with the program exiting in response to the input from in. txt.

I want to have a transcript that shows both input and output. That is, suppose my program displays the prompt "\ nEnter the number:", accepts the number entered by the user and displays its double value, "\ nEnter your number:" and continue to do this until the user enters 0.

Say I have an in.txt containing:

one
3
0

Then I want to have I / O decryption:

Enter the number: 1
Twice your number: 2
Enter the number: 3
Twice your number: 6
Enter the number: 0
Twice your number: 0

Sorry if I didnโ€™t explain it very well ... I really didnโ€™t know how to say it.

Is there a way to make this simple, or do I just need to enter the input manually ... and save the terminal a bit ...

+8
c ++ redirect input console
source share
3 answers

script does not apply to your specific use case. You would like to see input and output in your program in the same way as the user would see it, but without having to do it yourself.

I found Expect , which seems to be exactly what we are looking for. I do not know Tcl, but there is a Python port, pexpect . You 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 to the 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 this:

 transcript ./executablefile fileforinput 

My mileage sample gave me this conclusion:

 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 correctly, this should be the exact answer you are looking for. And it works in any program without any changes.

Hope this helps!

-Jake

+3
source share

The UNIX script command will do this.

+2
source share

Interest Ask. Something that should be cross-platform is similar to the example below (I tested on Windows and * nix, but unfortunately did not test mac for testing). In principle, you will read the source file and extract its data (in the case of the example, it is assumed that the file is formatted exactly the same as you mentioned above) and somewhere this data is stored. Then write the data back to the file from which you read it.

 /** * File Input/Output * * Input file is also output file * * 12/13/10 */ #include <iostream> #include <fstream> #include <sstream> #include <string> using namespace std; int main() { // Get data and store it ifstream in("output.txt"); // Simple error checking if(!in.is_open()) { cout<< "There was an error opening output.txt!" << endl; return 0; } cout<< "Reading file..." << endl << endl; // Strings are quicker to implement string tmp; string data; int tmpi = 0; // Here is where we store the input - the stringstream allows us to handle // multiple variable types and convert them. NOTE: there is no error checking here, // so wrong types _WILL_ throw errors (format needs to be one number per line) stringstream ss(stringstream::in|stringstream::out); while(getline(in,tmp)) { tmpi = 0; // Reset ss.str(string()); ss << tmp; ss >> tmpi; tmpi *= 2; // Reset it again so we can get the doubled value ss.clear(); ss.str(string()); ss << tmpi; data.append("Enter a number: "+tmp+"\r\n"+"Twice your number is: "+ss.str()+"\r\n"); } in.close(); // Output handling ofstream out("output.txt",ios::binary|ios::trunc); // Delete everything which was in the file? // Simple error checking if(!out.is_open()) { cout<< "There was an error opening output.txt!" << endl; return 0; } cout<< "Generating output..." << endl << endl; // Write to the file out.write(data.c_str(),data.size()); out.close(); cout<< "Done!"<< endl; cin.get(); // Pause momentarily return 0; } 

My original input file:

 12 2312349 324843 3249 0909 

The output was:

 Enter a number: 12 Twice your number is: 24 Enter a number: 2312349 Twice your number is: 4624698 Enter a number: 324843 Twice your number is: 649686 Enter a number: 3249 Twice your number is: 6498 Enter a number: 0909 Twice your number is: 1818 

Hope this helps!

Good luck
Dennis M.

0
source share

Source: https://habr.com/ru/post/650082/


All Articles