Question

//Checks if an argument was specified
if (argv[1] != "")
    strcpy(Buff1, argv[1]);
else
    strcpy(Buff1, "default");

If I run: ./program test

Buff1 = test

If I run: ./program

Buff1 = PACKAGES/=packages

How do I make it if nothing was specified, that Buff1 would be "default" by default?

Was it helpful?

Solution 3

Use argc to determine arguments count. It will be equal to 1 if no arguments were given, and 2 if one argument was given.

Note that you can't compare C strings using == operator. It's pointers comparasion.

OTHER TIPS

The argc gives You the number of arguments passed to the program. Keep in mind that argc can not be less than 1 since argv[0] is always program's name so if there were no arguments passed You should use this if(argc == 1){}

Use argc for knowing how main arguments are passed. Shell or process invoker feeds the program with atleast one argument in general, that is program name itself and that is always the first argument. It turns out that argc=1 atleast, and argv[0] is program name.

  int main(int argc, char **argv){
      // declarations and all here
      if(argc<2){
         strcpy(Buff1, "default");

        }
       else{
          strcpy(Buff1, argv[1]);
      }
     return 0;
  }

With out using this, you have two problems. When you use argv[1], when argc=1, you are actually going array out of bounds. Since, c++ doesn't do any bounds check for you, sometimes your program might fail silently accessing memory address next to the argv[0]. And another problem is that you are trying to compare the strings with != operator. You can't compare string literals right away with ==/!= operator. You have to use strcmp or an equivalent function.

Okay, if nothing is passed, argc is going to 1 (argc gives the number of passed arguments). This means that the only argv element with anything in it will be argv[0] (which contains the name of the program). That means a call to argv[1] will be an index out of range, possibly causing a crash, or if you're lucky will just be junk data.

if(argc == 1)
   strcpy(Buff1, "default");

else if(argc == 2)
    strcpy(Buff1, argv[1]);

else
    //do something here if there is more than 1 argument passed to it

It's also worthwhile to note that the way you passed the example arguments would not work with what you're intending: "./program test Buff1 = test" would result in argc being 4, with argv[0] being "test", argv[1] being "Buff1", argv[2] being "=" and argv[3] being "test".

Simply calling "./program test helllooo" would work with the program snipit I provided, filling Buff1 with "helllooo". And calling "./program test" would also work, filling Buff1 with "default". To do anything more advanced, you're going to have to get into command line switches (like ./program test -b somethinghere -x somethinghere), which is just a more advanced way of parsing argc and argv.

suggestion: use argv.size() to get the count.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top