Command line arguments are used when you run your program from the OS command prompt:
c:\>myprog.exe filename.txt
Here a program named myprog has an argument that is a file name. Maybe the program will read or write the file. Without this, the program would need you to enter the file name as part of the program.
Basically, any data the program needs in order to run can be passed as a command line argument. These arguments are passed to main:
- int main(int argc, char* argv[])
-
{
-
-
-
}
argc is the number of arguments. In our example, there are 2 arguments:myprog.exe and filename.txt. You use argc to tell you which arguments you are using:
argv[0] is myprog.exe
argv[1] is filename.txt
In the program if you fopen argv[1] you will be opening filename.txt.