1

How do I access the command line argument that I set in my project. for example if I give following input to command line "abc def ghi" then how do I access them using argc &/or argv.

I am getting some integer values if I am accessing them via argv[i] or *argv[i] Thanks.

Pawan Tejwani
  • 109
  • 1
  • 1
  • 6

1 Answers1

1

You can access them like in this example:

#include <iostream>

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

The cause of the error you are experiencing probably is that you use the TCHAR form of the main function:

int _tmain(int argc, _TCHAR* argv[]);

In Visual C++ per default UNICODE is defined. Therefore you have to use std::wcout for output instead of std::cout.

This is the way it probably will work:

  for(int i=0;i<argc;i++) {
   std::wcout<<argv[i];
  }
Norbert Willhelm
  • 2,579
  • 1
  • 23
  • 33
  • 1
    thanks I got the mistake. Instead of using _tmain and _TCHAR I used main and char. and I got to know one more point that: by default first command line argument is **path to .exe file** ... thanks :) – Pawan Tejwani Sep 25 '11 at 18:41