Yes
1 2 3 4 5 6 7 8 9
|
int main(int argc, char **argv)
{
argv[0] //this is the first command - it will always be the name of the program
//("program" or "program.exe" on windows, and "./program" on Linux)
argv[1] //this is the first command entered by the user AFTER the name of the program
argv[2] //the second command, and etc
return 0;
}
|
For example, say I'm using windows and I launch my program called "MyProgram"
Command Prompt:
C:\Users\Me> MyProgram.exe 100 Lines Of Code
Now Windows invokes the program, feeding "MyProgram.exe" as "argv[0]", "100" as "argv[1]", "Lines" as "argv[2]", "Of" as "argv[3]", and "Code" as argv[4].
You don't have to worry about going out of bounds because "argc" will ALWAYS tell you how many arguments are being provided when the program is invoked.
I have a program called "Maze.exe" that traverses a maze in a text file.
Here is how I invoke in from the command line:
--------------------------------------------------------------------------------------------------------------
C:\Users\********\Documents\Visual Studio 2013\Projects\Maze\Debug>dir
Volume in drive C is OS
Volume Serial Number is ****-****
Directory of C:\Users\*********\Documents\Visual Studio 2013\Projects\Maze\De
bug
11/20/2014 11:06 PM <DIR> .
11/20/2014 11:06 PM <DIR> ..
11/20/2014 10:48 PM 56
input.txt
11/20/2014 11:06 PM 134,656
Maze.exe
11/20/2014 11:06 PM 783,920 Maze.ilk
11/20/2014 11:06 PM 1,708,032 Maze.pdb
4 File(s) 2,626,664 bytes
2 Dir(s) 288,124,624,896 bytes free
C:\Users\Kurt Slagle\Documents\Visual Studio 2013\Projects\Maze\Debug>
Maze.exe i
nput.txt -dfs
#I'm not including the output from my program - it's functionality is not important here
I called the program with "Maze.exe", which becomes "argv[0]", I passed "input.txt" as the next argument, which becomes "argv[1]" and I passed "-dfs" as the last argument, which becomes "argv[1]".