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

发布于 2024-10-07 07:45:25 字数 598 浏览 3 评论 0原文

我有一个 C++ 程序,它输出提示并通过标准输入流 cin 获取用户输入。

我想获得完整的记录,包括程序的输出和文件中的输入。

我知道我可以使用命令行重定向来重定向输入/输出(即 ./program < in.txt > out.txt),但这只会用程序的输出填充 out.txt 以响应来自 in 的输入。 TXT。

我想要一份显示输入和输出的文字记录。也就是说,假设我的程序输出提示“\n输入数字:”,接受用户输入的数字并输出其双倍,“\n你的数字是:”的两倍,并继续执行此操作,直到用户输入 0。

假设我的 in.txt 包含:

1
3
0

然后我想要一份输入/输出的记录:

输入数字:1
您的数字是:2
的两倍 输入数字:3
您的数字是:6
的两倍 输入数字:0
您的数字的两倍是:0

抱歉,如果我没有很好地解释这一点...我真的不知道如何措辞。

有没有办法简单地做到这一点,或者我只需要手动输入输入...并保存终端...

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

望笑 2024-10-14 07:45:25

script 不涵盖您的确切用例。您希望看到程序的输入和输出与用户看到的完全一样,但不必自己动手。

我找到了 Expect,这似乎正是我们正在寻找的。我不知道 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

然后将此代码复制到可执行文件中:

#! /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,

然后您可以像这样运行它:

transcript ./executablefile fileforinput

我的示例运行给了我这个输出:

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

假设我正确阅读了您的问题,那应该是您的确切答案寻找。它适用于任何程序,无需任何修改。

希望有帮助!

-杰克

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

二智少女猫性小仙女 2024-10-14 07:45:25

UNIX script 命令可以做到这一点。

The UNIX script command will do it.

泼猴你往哪里跑 2024-10-14 07:45:25

有趣的问题。应该是跨平台的东西就像下面的例子(我已经在 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;
}

我的原始输入文件是:

12
2312349
324843
3249
0909

输出是:

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

希望这有帮助!

祝你好运!
丹尼斯·M.

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.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文