Command Line Arguments

May 2, 2011 at 12:42pm
Hi

I am really struggling to use command line arguments. I have finished the rest of my program but I have to do this to enter the file path to a txt file as well as coordinates (starting point for a maze) in the format:

filepath x y

I understand so far that you must start like this:

1
2
3
4
int main (int argc,char *argv[]) 
{ 

}


however I am not sure how to enter the inputs and at the moment I am just trying to use trial and error to work it out however isn't working for me (including any website that I use!) I would really appreciate it if someone can at least point me in the right direction please!
May 2, 2011 at 1:39pm
The variables argc and argv are filled in by the operating system when you specify options to your program on the command line. In your example, filepath x y, your variables would be this:
1
2
3
4
5
6
7

argc = 3.

argv[ 0 ] = "filepath"
argv[ 1 ] = "x"
argv[ 2 ] = "y"


argv[ 0 ] is always the program name (either the full path or just the program name). argc is the number of arguments you passed (2 in your case) plus one (for your program name).

There are plenty of examples that you can find to take from here.
May 2, 2011 at 2:48pm
I'm not sure how much you know about using the variables argc and argv so I'll start at the top. Apologies if I cover something you already know, I don't mean to be condescending :)

argc: Gives you the total number of parameters passed to your application.
argv: Character string of all the parameters passed.

To access a specific parameter:
argv[x]; //where x is the index of the desired parameter

It's important to remember that argc begins at 0. Also argv[0] will always be the full path and file name of your application.

I hope this helps!
May 2, 2011 at 2:52pm
One other point, really just about arrays in general. Like lnk2019, I don't mean to be condescending - just ignore this if you already know about it.

Remember that you will get runtime errors if you access an element beyond the bounds of an array (e.g. trying to access the fourth element of an array of size three). And the particular relevance to this situation? Don't assume your program gets all the command line arguments it needs. Check 'argc' to be sure 'argv' contains all the element you want before attempting to access them.

Hope this helps.
May 2, 2011 at 3:33pm
Appreciated for all the help guys but I still don't fully understand. I understand that argc is the number of arguments in the command line and i also understand argv. However I don't understand how you input strings on the command lines. To make it clear what I am trying to do I will write it in the way I have been previously been taught in c++:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

int main ()
{
	int x;
	int y;

	cout << "Please enter x and y coordinates: ";
	cin >> x >> y;

	cout << "x = " << x << endl << "y = " << y << endl;

	return 0;

}


I think what my question is how do you input on the command line so you can put it in a variable. Cheers for the help! :)

May 2, 2011 at 3:37pm
The above example won't work. You need to tell you application to be ready to except arguments to "main(...)".

You know how you use "ipconfig"? You know how you can pass it command switches such as "/all" and "/flushdns"? Argv[1] would be that command line switch in your application.

Another example would be "ping". To ping google you would type "ping www.google.com" the part after the space (www.google.com) is your first argument to the program, or in other words it is what argv[1] would point to.
Last edited on May 2, 2011 at 3:38pm
May 2, 2011 at 3:46pm



1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream> 
#include <cstdlib> 
using namespace std;

int main(int argc, char* argv[]) { 

int x = atoi(argv[1]);
cout << "x=" << x << endl;
int y =  atoi(argv[2]);
cout << "y=" << y << endl;

return 0; 
}



Command line used in bold, output following.
j@j-desktop: ./a.out 4 6
x=4
y=6
Last edited on May 2, 2011 at 3:47pm
May 2, 2011 at 3:51pm
So you would open the application in command prompt and then have the arguments after for example with x and y coordinates in the command line:

C:\\maze 5 6

Would that be how you would use it?
May 2, 2011 at 3:53pm
Yes that's the idea. Many IDEs also allow you to put the arguments in via some option somewhere, for ease of debugging.
May 2, 2011 at 3:54pm
Assuming you're using Windows and that is the directory of the application, then yes, that looks right except for the extra '\'.
May 3, 2011 at 2:17am
Also argv[0] will always be the full path and file name of your application.


This is not true.

argv is just whatever was on the commandline. That means argv[0] is likely whatever command was used to launch your program, which typically will be the program name but it doesn't have to include the full path and often doesn't.
Topic archived. No new replies allowed.