Argc and Argv

My professor has posted an assignment dealing with argc and argv input/output files, but I need some clarity on how I should approach it.

Have the program argcArgv require two inputs, an input file and an output file; neither of which can be cin or cout. This means that, from the command line, the user must enter the names of the input and output files as parameters to the program, as in:

argcArgv /i=war.txt /o=peace.txt

Once the program has both the input and output files, the input file will then be copied to the output file. The program must warn the user if the output file already exists and inquire if the file is to be overwritten, unless one of the following options are given as the last parameter on the command line:

/e -> overwrite

/a -> append

This means that the input and output file names must be copied from argv to separate variables BEFORE you can determine if the file needs to opened for appending or not. If the /e option is not given, then the program should error out.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <QtCore/QCoreApplication>
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>

using namespace std;

void parseCommandLine(int argc, char* argv[], istream &in, ostream &out);
void copyFile(istream &in, ostream &out);

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    istream input;
    ostream output;
    parseCommandLine(argc, argv, input, output);

    exit(0);copyFile(input,output);
    return a.exec();
}

void parseCommandLine(int argc, char* argv[], istream &in, ostream &out)
{
    ofstream output;
    ifstream input;
    in = cin;
    out = cout;
    if( argc < 2)
    {
        cout<<"Useage: argvArgc [/o=filename] [/i=filename]"<<endl;
        exit(1);
    }
    for(int i = 1; i < argc; i++)
    {
        if(argv[i][0] != '/')
        {
            cout<<"Illegal operand. Program terminating"<<endl;
            exit(1);
        }
        switch(toupper(argv[i][1]))
        {
            case 'I' : input.open(argv[i][3]);
                       if(input.fail())
                       {
                           input.close();
                           input.clear();
                           cout<<"Input file does not exist!"<<endl;
                           exit(1);
                       }
                       in = input;
                       break;

            case 'O' : output.open(argv[i][3]);
                       out = output;
                       break;

             default : cout<<"Illegal option. Program terminating"<<endl;
                       exit(0);
        }
    }
}

void copyFile(istream &in, ostream &out)
{
    char data;
    while(in>>data)
        out<<data;
    if(in != cin)
        in.close();
    if(out != cout)
        out.close();
}
Instead of passing an istream and an ostream to parseCommandLine, why not just get a string containing the file names? You also need to check for /e and /a and should return something so you know if one of them was used.

Don't use exit(). It bypasses the natural operation of your program. Just return true or false from your functions as to whether they were successful or not, and return from main. Also, lines 39 and 49 have exit(1). Error codes are for if your program breaks, your program is doing exactly what it should in those situations so you should return 0.
So instead of istream and ostream I replace it with string title, string title2 and have the user enter the name of the file


I am confused as to what /e and /a is, /e is refering to the input file and /a is refering to the output file?
alex067 wrote:
/e -> overwrite
/a -> append


Get the filenames from argv, and return them to main as strings you can use in another function.
I understand that, but I am at a loss as to what /e overwrite and /a append means
I am at a loss as to what /e overwrite and /a append means

It refers to how the output file is to be treated.

Quote from opening post:
The program must warn the user if the output file already exists and inquire if the file is to be overwritten, unless one of the following options are given as the last parameter on the command line:

/e -> overwrite

/a -> append

If the output file does not already exist, neither of these options will apply (perhaps a warning message might be issued) as the only possible action is to create a new output file.

But if the output file does already exist, there are three possibilities:
1. third parameter is omitted
- issue a warning message.
- inquire if the file is to be overwritten.
2. third parameter is /e - create a new output file which will completely replace the previous file.
3. third parameter is /a - keep the existing file, open it in "append" mode, that is the new data will be added after the end of the existing data.

Edit: I though I'd understood this, but I'm not sure under what circumstances this is supposed to apply: "If the /e option is not given, then the program should error out." My assumption is that it applies to case 1, after asking the user whether or not to overwrite the file. If the user replies "no", then error out.
Last edited on
How would the code be constructed? i managed to have the user input the address of the file, yet how do i compare them both?

#include <QtCore/QCoreApplication>
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>

using namespace std;

void parseCommandLine(int argc, char* argv[], string &input, string &output);
void copyFile(istream &in, ostream &out);
void getFile(char* title, char *title2, string &input, string &output);
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    string infile;
    string outfile;
    getFile("Enter the name of the input file", "Enter the name of the output file", infile, outfile);

    parseCommandLine(argc, argv, infile, outfile);

    exit(0);
    copyFile(infile, outfile);
    return a.exec();
}

void getFile(char* title, char title2, string infile, string outfile)
{
    cout << title;
    cin.getline(infile);

    cout << title;
    cin.getline(outfile);

}

void parseCommandLine(int argc, char* argv[], string& input, string& output)
{
    cout<<input;
    cout<<output;
    if( argc < 2)
    {
        cout<<"Useage: argvArgc [/o=filename] [/i=filename]"<<endl;
        exit(1);
    }
    for(int i = 1; i < argc; i++)
    {
        if(argv[i][0] != '/')
        {
            cout<<"Illegal operand. Program terminating"<<endl;
            exit(1);
        }
        switch(toupper(argv[i][1]))
        {
            case 'I' : input.open(argv[i][3]);
                       if(input.fail())
                       {
                           input.close();
                           input.clear();
                           cout<<"Input file does not exist!"<<endl;
                           exit(1);
                       }
                       in = input;
                       break;

            case 'O' : output.open(argv[i][3]);
                       out = output;
                       break;

             default : cout<<"Illegal option. Program terminating"<<endl;
                       exit(0);
        }
    }
}

void copyFile(istream &in, ostream &out)
{
    char data;
    while(in>>data)
        out<<data;
    if(in != cin)
        in.close();
    if(out != cout)
        out.close();
}

Last edited on
How would the code be constructed? i managed to have the user input the address of the file, yet how do i compare them both?

I don't think you need the function getFile() at all. The two file names will be supplied as parameter on the command line when running the program, there's no need to ask for the same information all over again. See my previous post for some ideas of the logic required.

You have exit(0); before calling function copyFile() so that code will never be executed. Also you seem to be passing two strings as parameters, but the function expects two streams.

You may also find this thread useful, the post by JLBorges gives an efficient way to copy a file. http://www.cplusplus.com/forum/beginner/132474/#msg712830
Topic archived. No new replies allowed.