Help with Command Line Arguments

Hey guys I have an assignment where the following will occur:

The user will input on a command line:

./a.out inputFile1.txt inputFile2.txt inputFile3.txt outputFile.txt verbosity

I understand how these will all work except for the last one: verbosity.

In place of verbosity, the user will type either "vOne", "vTwo", or "vThree". Depending on this word I will output a different text file.

How do I implement this in my code? Is the solution this?:

int main(int argc, char* argv) {
ifstream<<textFile1(argv[1]);
ifstream<<textFile2(argv[2]);
ifstream<<textFile3(argv[3]);
ofstream<<outputFile(argv[4]);
string verbosity = (argv[5]);
}

and if it is, then what should I do in my source code concerning the verbosity level? I am not asking you to provide me the code, I am just trying to figure out how command line arguments translate to the source code.

Thank you!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <fstream>
#include <string>

int main( int argc, char* argv[] )
{
    // validate argc, argv, display usage if invalid etc.
    if( argc != 6 ) return 1 ;

    std::string verbosity = argv[5] ; // *** EDIT: missed argument for output file
    std::string file_name ;

    if( verbosity == "vOne"  ) file_name = argv[1] ;
    else if( verbosity == "vTwo"  ) file_name = argv[2] ;
    else if( verbosity == "vThree"  ) file_name = argv[3] ;
    else { /* error */ return 1 ; }

    // error handling (could not open file etc.) elided for brevity
    std::ofstream( argv[4] ) << std::ifstream(file_name).rdbuf() ; // *** EDIT ***
}
Last edited on
Topic archived. No new replies allowed.