Question

So my main function looks like this:

int main(int numberOfArguments, char* argumentArray[]) {
        if (assemble(numberOfArguments, argumentArray)) {
                return 0;
        } else {
                return -1;
        }
}

I want to be able to give to the command line the arguement

./program inputFile.txt outputFile.txt // PROBLEM IS that this is being interpreted as 3 command line arguments somehow when it is only 2.

where inputFile.txt is argumentArray[0] and outputFile.txt is argumentArray[1]

Was it helpful?

Solution

The generation of arguments from the text command line is platform-specific, but the first element of the argv array is almost always the name used to find your program.

If you want to discard this first argument, simply add to the beginning of main:

-- numberOfArguments;
++ argumentArray;

OTHER TIPS

every word you type into the command line (beginning with the executable name) is passed to the main function at begin. related question

The first argument argumentArray[0] is the as readable in the realted question reserved. After that you get each word (seperated by whitespaces in the commandline) as an element in the argumentArray.

The size of the argumentArray is given by numberOfArguments.

So if you insist on having your input file on [0] you should work with some pointer arithmetics like argumentArray++; otherwise you have the given data at [1] and [2] automatically.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top