How to use command line parameters

Asked

Viewed 2,300 times

-1

I’m doubtful about the use of the command line parameters, like, one of the functionalities of the university’s job is to treat some arguments per command line, for example:

./program -e -i file.txt fileDestino.txt

I don’t know how to use larger arguments, except the "-e" options for example, I wonder how I can call a text file on the command line or something like

  • Take a look here and see if it solves.

2 answers

2

There are many ways to do this, you can check each past argument and when a given argument is that specific just take the next parameter and then skip it to not be checked. an example would be if your program checks the age passed in the -i parameter, when the program finds the -i it takes the next argument after -i which is the age and then skips the next argument in the counter not to check the age value passed as argument

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char **argv){
   int contParam, idade = 0;
   //corre cada paramatro passado
   for(contParam = 0; contParam < argc; contParam++){
      //se o parametro passado for -i ele executa isso
      if(!strcmp(argv[contParam],"-i")){
         //armazena na variavel local o valor passado
         //que seria o paramatro + 1
         idade = atoi(argv[contParam + 1]); //pega o proximo valor
         contParam += 1; //checa outro parametro desconsiderando
                         //o proprio valor como parametro
      }
   }

   printf("idade = %d \n",idade);
}

in the previous example if you pass to the program -i 10 it will display the age 10

kodo.exe -i 10

if you pass another value to it before the minus -i, the result will be the same because the program picks up the value after -i

kodo.exe 50 -i 10

the same is true if the value is passed after the value of -i

kodo.exe 50 -i 10 80

of course if you pass another -i will overwrite the value in the variable, the good that this allows you to pass parameters to your program without a specific order too

kodo.exe 50 -i 10 80 -i 30

another example now passing the name in the -n parameter, age in -i and date in -d

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char **argv){
   int contParam, idade = 0;
   char nome[100] = "", data[20] = "";

   for(contParam = 0; contParam < argc; contParam++){
      if(!strcmp(argv[contParam],"-i")){
         idade = atoi(argv[contParam + 1]);
         contParam += 1;
      }

      else if(!strcmp(argv[contParam],"-n")){
         strncpy(nome,argv[contParam + 1],100);
         contParam += 1;
      }

      else if(!strcmp(argv[contParam],"-d")){
         strncpy(data,argv[contParam + 1],20);
         contParam += 1;
      }
   }

   printf("nome = %s \n",nome);
   printf("idade = %d \n",idade);
   printf("data = %s \n",data);
}

the command on the terminal would be

kodo.exe -d "25/06/2017" -n "kodo no kami" -i 20 

the exit in the terminal would look like this

nome = kodo no kami 
idade = 20 
data = 25/06/2017
  • 1

    Great answer; perhaps it is only worth adding that if the program determines that a certain element of the vector argv should be interpreted as a file name, it can be opened normally using the read file API (FILE * stream = fopen(argv[n], "r"); or int fd = open(argv[n], O_READ);)

  • By the way, welcome to Stack Overflow in Portuguese! I hope you have the opportunity to contribute more...

1

Make use of the arguments argv and argc.

argv and argc are how command line arguments are passed to main() in C and C++.

argc will be the number of strings pointed out by argv. In practice, this will be 1 plus the number of arguments, since virtually all implementations add the program name to the array.

The variables are called argc (argument Count) and argv (argument vector) by convention, but may receive any valid identifier: int main(int num_args, char ** arg_strings) is equally valid.

They can also be omitted entirely, producing int main(), if you do not intend to process command line arguments.

Try the following code:

#include <stdio.h>

int main(int argc, char **argv)
{
  printf("Existem %d argumentos\n", argc);

  for (int i = 0; i < argc; i++)
  {
    printf("Argumento [%d]: %s\n", i, argv[i]);
  }

  return 0;
}

Running with ./programa -e -i arquivo.txt arquivoDestino.txt the exit will be

Existem 5 argumentos
Argumento [0]: ./programa
Argumento [1]: -e
Argumento [2]: -i
Argumento [3]: arquivo.txt
Argumento [4]: arquivoDestino.txt
  • It’s not that "...virtually all implementations add the program name to the array "; the pattern orders the value of argc is greater than zero, argc[0] has to represent the name of the program.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.