是否有办法将输入和输出重定向到同一个文件?

时间:2021-10-18 03:12:25

I have a C++ program that outputs prompts and takes user input via the standard input stream cin.

我有一个c++程序,它通过标准的输入流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.

我知道我可以用命令行重定向输入/输出(例如./program < in)。txt >输出。txt与程序的输出响应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.

我想要一个显示输入和输出的文本。也就是说,假设我的程序输出一个提示“\nEnter一个数字:”,接收一个用户输入的数字并输出它的double,“\ n2你的号码是:”,并一直这样做,直到用户输入一个0。

Let's say I have in.txt containing:

假设我有。txt包含:

1
3
0

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

输入一个数字:1倍你的数字是:2输入一个数字:3倍你的数字是:6输入一个数字:0倍你的数字是: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...

有没有一种简单的方法,或者我只需要手工输入输入……然后保存终端…

3 个解决方案

#1


3  

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:

我找到了期待,这似乎正是我们所期待的。我不知道Tcl,但是有一个Python端口,pexpect。您需要安装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

杰克

#2


2  

The UNIX script command will do it.

UNIX脚本命令将执行此操作。

#3


0  

Interesting question. Something which should be cross platform is like the example below (I have tested on Windows and *nix but do not have a mac to test on unfortunately). Basically, you will read the initial file and extract its data (in the case of the example, it assumes that the file is formatted exactly as you have mentioned above) and store that data somewhere. Then, write the data back to the file from which you read it.

有趣的问题。有些东西应该是跨平台的,比如下面的例子(我在Windows和*nix上测试过,但不幸的是没有mac来测试)。基本上,您将读取初始文件并提取它的数据(在本例中,它假设文件的格式与前面提到的完全一样),并将该数据存储在某处。然后,将数据写回读取数据的文件。

/**
 * 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 was:

我最初的输入文件是:

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.

好运!丹尼斯·M。

#1


3  

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:

我找到了期待,这似乎正是我们所期待的。我不知道Tcl,但是有一个Python端口,pexpect。您需要安装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

杰克

#2


2  

The UNIX script command will do it.

UNIX脚本命令将执行此操作。

#3


0  

Interesting question. Something which should be cross platform is like the example below (I have tested on Windows and *nix but do not have a mac to test on unfortunately). Basically, you will read the initial file and extract its data (in the case of the example, it assumes that the file is formatted exactly as you have mentioned above) and store that data somewhere. Then, write the data back to the file from which you read it.

有趣的问题。有些东西应该是跨平台的,比如下面的例子(我在Windows和*nix上测试过,但不幸的是没有mac来测试)。基本上,您将读取初始文件并提取它的数据(在本例中,它假设文件的格式与前面提到的完全一样),并将该数据存储在某处。然后,将数据写回读取数据的文件。

/**
 * 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 was:

我最初的输入文件是:

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.

好运!丹尼斯·M。