【问题标题】:is there a way to redirect the input and output to the same file?有没有办法将输入和输出重定向到同一个文件?
【发布时间】:2011-05-24 14:03:10
【问题描述】:

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

我想获得一个完整的脚本,包括程序的输出和文件中的输入。

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

我想要一份同时显示输入和输出的成绩单。也就是说,假设我的程序输出提示“\n输入一个数字:”,获取用户输入的数字并输出其双精度,“\n您的数字是:”,并一直这样做直到用户输入一个 0。

假设我有 in.txt 包含:

1
3
0

然后我想要输入/输出的成绩单:

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

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

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

【问题讨论】:

  • Mac、Linux,两者都有。我很乐意为任何操作系统提供解决方案
  • 为什么不在输入后立即输出所有输入内容?
  • 我想我可以做到,我有点希望有一个不涉及修改我的程序的解决方案。

标签: c++ redirect input console


【解决方案1】:

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

假设我没看错你的问题,那应该就是你要找的确切答案。它可以在任何程序上运行而无需任何修改。

希望有帮助!

-杰克

【讨论】:

    【解决方案2】:

    UNIX script 命令会执行此操作。

    【讨论】:

    • hmm,脚本只是记录我程序的输出,而不是输入+输出。有没有办法使用脚本并重定向文件中的输入?
    • 这不符合他的要求
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-06
      • 2012-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多