简体   繁体   中英

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). 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 am fairly new to Linux and not sure how to do this in a efficient manner. 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. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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