Friday, January 18, 2013

Basic examples of using argc and argv[]



#include <iostream>

using namespace std;

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

cout << "argc :" << argc << endl;

for(int i=0;i<argc;i++){
    cout << "argv[" << i << "]: " << argv[i] << endl;
}

return 0;
}

I'm using Visual Studio 2012. After build solution (Ctrl+Shift+b), you will find a debug folder under you project folder. Find out the path of the application you just built, then you will be able to run it using windows cmd.
At the command line, type C:\path\Debug\application.exe one 2 three, the output will be:
argc: 4
argv[0]: C:\path\Debug\application.exe
argv[1]: one
argv[2]: 2
argv[3]: three

No comments:

Post a Comment