Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Home » Tip Bank » C++
Language: C++
Expertise: Beginner
Jun 29, 1999

Command Line Arguments

Question:
I have a small console program that requires I pass arguments for it to run specific functions. I want to change the code so it automatically runs a set of command switches but cannot figure out how to do so. If I do
argv[0] = "-a -b -c" 
I get all kinds of errors.

Answer:
I don't know what your code is doing but argv[0] should produce a valid string. The code snippet below iterates through the list of command line arguments. Perhaps you can use this as a starting point to get the information you need. Note that argv[0], the first argument, is always the name of the executable file.

#include 

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

   for (i = 0; i < argc; i++) {
      printf("Arg %d = %s\n", i, argv[i]);
   }
   return 0;
}
DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date