c++ - 打开文件进行 I/O 并使用命令行重定向

标签 c++ linux

我正在编写一个程序,该程序从文本文件(C++ 代码)获取输入并修改它以在文本文件(html)中输出。该程序需要从标准输入读取并写入标准输出。它使用命令行参数 -i filename 作为输入,使用 -o filename 作为输出以及 shell 输入/输出重定向。我对 Linux 相当陌生,不知道如何有效地做到这一点。这是我到目前为止得到的:

#include <fstream> 
#include <iostream>
#include <cstdlib>
#include <vector>
#include <string.h>
#include <string>

using namespace std;

int main(int argc, char * argv[]) {
     vector<string> mod;
     int modNum = 0;
     int i = 1;   
     string input = "";
     string inFilename = "";
     string outFilename = "";
     ifstream inFile;
     ofstream outFile;

     while(i < argc){
        if (strcmp(argv[i],"-i") == 0 ) {
                i++;
                if (i<argc){
                    inFilename = argv[i];
                } else {
                    cout << "\t-i require a filename for input" << endl;
                }
                i++;                
        } else if (strcmp(argv[i],"-o") == 0 ) {
                i++;
                if (i<argc){
                    outFilename = argv[i];
                } else {
                    cout << "\t-i require a filename for output" << endl;
                }
                i++;
        }
    }
    if (inFilename != "" ) {
        inFile.open(inFilename.c_str());
    }       
    if (outFilename != "") {
        outFile.open(outFilename.c_str());
    }    
    if (inFile.is_open() and outFile.is_open()) {
        outFile << "<script src=\"https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js\"></script>" << endl;
        outFile << "<pre class=\"prettyprint\">" << endl;
        while (!inFile.eof()) {
            getline(inFile, input);
            outFile << input << endl;             
        }
        outFile << "</pre>";
    } else {
        cout << "<script src=\"https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js\"></script>" << endl;
        cout << "<pre class=\"prettyprint\">" << endl;
        while (cin) {
            if (getline(cin,input)) {
                cout << input << endl;
            }
        }
        cout << "</pre>";   
    }
    inFile.close();
    outFile.close();
}

我遇到的问题是这种方法没有通用性。如果我得到

./c < code.cpp -o page.html

./c -i code.cpp > page.html

程序无法正确执行。当我复制并粘贴代码块以仅显示必需品时,如果出现任何小错误,我深表歉意。该程序会做更多的事情,但现在我只是想让输入/输出正常工作。

最佳答案

到目前为止,您只处理了运行程序的四种情况中的两种:您的代码假设如果指定了 -i-o,则它从输入文件读取并打印到输出文件,否则它从输入读取并写入输出。因此,自然地,您的程序无法从文件读取并写入标准输出(反之亦然) - 代码不在这里。

我建议您在单独的函数中提取用于处理文件的代码,该函数将 istream& inputostream& output 作为参数,以避免代码重复。您将使用不同的参数来调用它,具体取决于您遇到的四种情况中的哪一种,例如:

process_file(inFile.is_open() ? inFile : cin, outFile.is_open() ? outFile : cout);

这将使您进一步纠正代码,但仍然不理想。我建议去codereview网站以获得更多反馈。

关于c++ - 打开文件进行 I/O 并使用命令行重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42080737/

相关文章:

c++ - 未定义对 clang 中函数指针的变量模板的引用,但不是 gcc

c++ - 通过指向实例的静态指针访问成员变量

c# - 用 Mono 编译 csharp 项目

linux - 当/dev/null对普通用户不可写时如何重定向stdout/stderr

c# - 尝试从 flatbuffer 的二进制文件访问 "LengthofTable"时出现 SystemAccessOutOfbound 异常

c++ - C++ 中赋值运算符和 C 字符串的问题

c++ - 具有使用共享 MFC dll 的 API 的 WinRT 应用程序

linux - Docker DNS 设置

linux - 使用 linux 将字符串 dd mm yyyy 转换为 yyyymmdd

c++ - cmake -- 让一个子目录使用它自己的 Makefile