Can't get passing through command line to work..

I read about some advanced technique called..."Command Line Arguments" it seemed pretty straightforward but I wanted to do a test to see what all it looked like when ran in an actual program.. So I built:
1
2
3
4
5
#include <iostream>

int main(int argc, char *agrv[]) {
    std::cout<<argv[0];
}

based off the examples provided in the book. Once I had the basic idea I was going to expand upon it from other books and resources to get a good idea what all uses it has.

First of all what is the purpose of them...and why isn't the above code compiling..it's returning:

||=== Test Project, Debug ===|
C:\xampp\htdocs\cpp projects\Test Project\main.cpp||In function `int main(int, char**)':|
C:\xampp\htdocs\cpp projects\Test Project\main.cpp|4|error: `argv' was not declared in this scope|
C:\xampp\htdocs\cpp projects\Test Project\main.cpp|4|warning: unused variable 'argv'|
||=== Build finished: 1 errors, 1 warnings ===|
typo make sure it's called arg + v and arg + c
argc, argv

edit: also make sure you test to make sure there is data inside the element before trying to reference it otherwise your going to get a stackdump.
you should store that data into a string as well.

How do we test if there is data inside if we can't reference it, referencing can cause the stackdump?
that answer can be found with another question....
What is argc used for ?

edit:2 referencing argv[0] is ok, as it is always the program name....
Last edited on
I appreciate that, thanks.
np, I had to explain this to a guy today in my semester 2 class, he still has not handed in the assignment from last year and it was due friday last week. Couldn't understand the assignment because he thought simply declaring file names would be easier than passing through commandline...

My god some people need to be shot, it was not until I said to him, will your program accept my file called "yousuck.dat" as input file? he said No, so I said how is that better to declare them inside the program then ?
I think he had an lightbulb moment, only it was 2 months too late. I told him he should probably re-enroll in semester 1 again because he wasn't going to pass being that it was last year and he still hasn't finished assesment, he didn't believe me but whatevers.

Anyhoo, I think you should have a go at this, basically what the program is required to do is take any number of arguments, but should be in the format of:

1
2
3
4
5
program_name   -i  inputFile.txt  -o  outputFile.txt

// or

program_name  -o  outputFile.txt  -i inputFile.txt

Note: the file names can be anything. Whatever the user wants to use as input and output.

your program is to parse the command line arguments and store the input file using the " -i " switch and output file using the " -o " switch.
duplicate file names are not allowed, and file names must have a length of at least 1. exit on errors.

The way I wrote mine, would take any number of arguments so if the user typed:
a.exe -i -i -i -i -i -o -i -p -o output.txt -o -o -o -j -i input.txt
it would still work as required. any invalid switches need to produce an appropriate error and exit program.

the reason the above would work is if you check every -i the following string is stored in the inputfile variable however it is overwritten by preceeding -i / -o switches.
the above is similar to this:
a.exe -i infile.txt -o outfile.txt -i foo.txt -i bar.dat -i foo.txt -i inputFile.txt -o oranges.txt -o outputFile.txt
Not to be confused with this:
a.exe -i infile.txt -p punchingbag
which should produce an error: Error switch [ -p ] unknown, usage: -i inFile -o outFile

after that the program was required to store the input file in an array, sort it and then put into output file.
However just doing the command line args was the most challenging part.

edit2: another note about that long strange argument list:
-o -j -i input.txt
"-j" would be stored as the file name for the output, however this is fine, it would not be until I opened input stored in array sorted array, and then went to open output and would get an error "Unable to open: -j, check file names and try again".
Last edited on
Topic archived. No new replies allowed.