How to use the parameters in the command line [duplicate]

-1

I'm in doubt as to the use of the parameters in the command line, type, one of the functionalities of the university work is to treat some arguments by command line, for example:

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

I do not know how to use larger arguments, except the "-e" options eg I would like to know how I can call a text file on the command line or something like

    
asked by anonymous 25.06.2017 / 20:54

2 answers

2

There are many ways you can do this, you can check each last argument and when that argument is specific you just have to get the next paramenter and then skip it to not be checked. an example would be if your program does a check of the age passed in the param -i, when the program finds the -i it takes the next argument after -i that is the age and then skips the next argument in the counter to not check the old age value 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 move to the -i 10 program it will display age 10

kodo.exe -i 10

If you pass it another value before 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 thing is that it allows you to pass parameters to your program without a specific order as well

kodo.exe 50 -i 10 80 -i 30

Another example now passing the name in the -n, age -i and date -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 in the terminal would be

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

the output on the terminal would look like this

nome = kodo no kami 
idade = 20 
data = 25/06/2017
    
25.06.2017 / 22:09
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 to 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 can 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 want 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 output will be

Existem 5 argumentos
Argumento [0]: ./programa
Argumento [1]: -e
Argumento [2]: -i
Argumento [3]: arquivo.txt
Argumento [4]: arquivoDestino.txt
    
25.06.2017 / 21:18