简体   繁体   English

为 I/O 打开文件并使用命令行重定向

[英]Opening files for I/O and using command line redirection

I am writing a program that takes input from a text file (c++ code) and modifies it for output in a text file(html).我正在编写一个从文本文件(c++ 代码)获取输入并修改它以在文本文件(html)中输出的程序。 This program needs to read from standard input and write to standard output.该程序需要从标准输入读取并写入标准输出。 It uses command line arguments -i filename for input and -o filename for output as well as shell input/output redirection.它使用命令行参数 -i 文件名作为输入,使用 -o 文件名作为输出以及 shell 输入/输出重定向。 I am fairly new to Linux and not sure how to do this in a efficient manner.我对 Linux 相当陌生,不知道如何以有效的方式做到这一点。 Here is what i got so far:这是我到目前为止所得到的:

#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();
}

The problem I am having is there is no versatility with this method.我遇到的问题是这种方法没有通用性。 If i get如果我有

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

or或者

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

The program won't execute correctly.程序将无法正确执行。 I apologize if there is any small errors as i copy and pasted chunks of code to show only the necessities.如果在我复制和粘贴代码块以仅显示必需品时出现任何小错误,我深表歉意。 The program will do more but right now I'm just trying to get input/output to work correctly.该程序将做更多的事情,但现在我只是想让输入/输出正常工作。

Well, so far you have processed only two out of four cases of running the program: your code assumes that if both -i and -o specified, then it reads from input file and prints to output file, otherwise it reads from input and writes to output.那么,到目前为止,您已经处理只有两个四例运行的程序:你的代码假定,如果同时-i-o指定的,那么它从输入文件和打印输出文件读取,否则它从输入和写入的读取输出。 So, naturally, your program cannot read from file and write to standard output (or vice-versa) - code for that is just not here.因此,自然而然地,您的程序无法从文件中读取并写入到标准输出(反之亦然) - 代码不在这里。

I'd suggest you extracting code for processing file in a separate function which takes istream& input and ostream& output as arguments to avoid code duplication.我建议您在一个单独的函数中提取用于处理文件的代码,该函数将istream& inputostream& output作为参数以避免代码重复。 You will call it with different arguments depending on which of the four cases you've encountered, something like:根据您遇到的四种情况中的哪一种,您将使用不同的参数调用它,例如:

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

This will get you further to correct code, but it's still not ideal.这将使您进一步更正代码,但仍然不理想。 I'd suggest going to codereview site to get more feedback.我建议去codereview网站以获得更多反馈。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM