argc and argv

I just dont get how the hell am i supposed to use them. all the examples i have seen show me that i should cout argc and argv before i ahve even done anything, but then where does it get ist command line lenght. could somone show me a complete example of a small prgoram what uses argc and argv and maybe a bit of explenation also. try using simple words, causen english isnt my native tongue and some parts are hard to understand.

http://www1.cs.columbia.edu/~aya/W3101-01/lectures/lec2/sld017.htm

compile this program and run like:

./program 36

the output should be 6.
this progrma doesent work, on compiling i get loads and loads of errors
Command Line Parameters are typically used to specify instructions and/or information to an application before it is run.

For example if you open the command prompt (run->cmd) and type notepad.exe myfile.txt, you have given the Command Line Parameter (myfile.txt) to the application notepad (which will cause it to attempt to open the file).

In C++ Command Line Parameters are understood using two variables:

argv is an array of c-string pointers.
argc specifies the number of elements within argv or the number of c-string pointers pointed to by argv.

For example:
if argc = 3; argv[0], argv[1] and argv[2] will contain data. If you try to invoke argv[3] in this context you will run into an error.

Example code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

using std::cout;
using std::endl;

int main(int argc, char* argv[])
{
    for(int i = 0; i < argc; i++)
    {
        cout<<argv[i] <<endl;
    }

    return 0;
}


This small application will print to the screen every command line parameter specified when it was run.

There was a previous discussion on this topic that you might find helpful you can find it here: http://www.cplusplus.com/forum/beginner/42115/

I hope this helps!
Last edited on
Then I think you might have a problem with your compiler... what are you using?

-Albatross
the first argument passed will be the program name. that is usually argv[0]. the below program takes 2 arguments the first a fictional filename and the second a fictional option. so you put the main program in your c:\ directory and navigate to that directory from the command line. once there you execute using the executable name then the 2 options. so you type main.exe myfilename myoption then enter. it should echo back and print the 2 options that you passed. so in total this program will receive 3 options. the program name the filename and the option. if it recieves anything more or less it prints its usage out to the command line.

btw its hard to read if you run it from anything other then your c drive since the path is so long when it prints the program name.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <limits>

int main(int argc, char *argv[])
{
	 using namespace std;

	 if ( argc != 3 )  // argc should be 2 for correct execution
         {
              // We print argv[0] assuming it is the program name
		 cout << "usage: " << argv[0] << " <filename> <option>\n ";
	 }
	 else
	 {
		 cout << argv[1] << " - argv[1]" << endl;
		 cout << argv[2] << " - argv[2]" << endl;
	 }

	cout << "Press ENTER to continue...";
	cin.ignore(numeric_limits<streamsize>::max(), '\n' );
	return 0;

}
sp basiclly i can do this. il write it in pseudo code

get input
if there are more or less than 2 element give error
else
compare argv[0] with (given parameter)
if true, argv[1] is filename
if false give error
end program.

so basiccly i can do this right??
Last edited on
The arguments passed to any C or C++ program are passed to it either by the operating system, or another program. By convention, argv[ 0 ] is the name of your program, and can either be a full-path name or a relative path name.

In your pseudo code, you start out by saying "get input". But actually if you are going to use argc and argv, then the user will envoke your program with the arguments.

Here is a simple example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14

int main( int argc, char** argv )
    {
    int i = 0;

    for( i = 0; i < argc, i++ )
        {
        printf( "Argv[ %d ] is (%s)\n", i, argv[ i ] );

        }    /*    for( i < argc )    */

    }
        /*    main()    */


Let's say this program is named mytest. The user could execute it like this:

mytest arg1 arg2 arg3

The output would be:

Argv[ 0 ] is (mytest)
Argv[ 1 ] is (arg1)
Argv[ 2 ] is (arg2)
Argv[ 3 ] is (arg3)
ok so i dont neeed input, but i still dont get where do i give it the arg1 arg2 and ar3 i must type em in somewhere, but where or with what am i supposed to run this. cause when i compile it and run it, then i get the full name of my file and thats it, nothing else.
its in different locations depending on the operating system but in windows 7 navigate to
start menu -> all programs-> accessories -> command prompt


once you run the command prompt you can start your executable by typing in the path to where the exe is or navigating to your c:\ directory and put your exe there
Last edited on
so in command promt i type in c\....\my_file 1 tere mina and this will produse the result of puut put
.... my_file
... 1
... tere
...mina
Yep!
Topic archived. No new replies allowed.