Assign variables from string entered by keyboard in C

0

I need to achieve the following:

The user will enter the following command by keyboard: cousins entry.txt -t -n 5

Where cousins is the exercise, input.txt a file that contains numbers, -t to indicate that threads will be created, and < strong> -n to indicate how many threads. Storing each of these data in an independent variable is what I need, and I've written the following code, but I can not make it work.

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

int main() {
    char cadena [100];
    printf ("\nLa instrucción, debe tener el siguiente formato:\n");
    printf("primos entrada.txt [-t | -p] [-n N] -> ");
    /* primos entrada.txt -t -n N */
    fgets (cadena, 100, stdin);

    //
    int longitud = strlen(cadena) - 1;
    char primos[6];
    char entradaTXT[10];
    char tipo[2];
    char cantidad[2];
    char n[2];
    // Ejercicio Primos
    fflush(stdin);
    for(int i = 0; i < 6; i++) {
        primos[i] = cadena[i];
    }

    // entrada.txt
    fflush(stdin);
    int j = 0;
    for(int i = 8; i < 18; i++) {
        entradaTXT[j] = cadena[i];
        j++;
    }
    // Tipo (Hilos o procesos)
    fflush(stdin);
    j = 0;
    for(int i = 20; i < 21; i++) {
        tipo[j] = cadena[i];
        j++;
    }
    // Cantidad de hilos o procesos
    fflush(stdin);
    j = 0;
    for(int i = 23; i < 24; i++) {
        cantidad[j] = cadena[i];
        j++;
    }
    //printf("\n%s",&primos[6]);
    printf("\n%s\n%s\n%s\n",&primos[6],&entradaTXT[10],&tipo[10]);
    fflush(stdin);
    return 0;
}

The result I get when compiling and executing is the following:

Apparently all the variables are being stored in a single chain, but I do not understand why, could someone give me a hand?

    
asked by Pedro Fumero 04.11.2017 в 17:17
source

1 answer

2
  

The user will enter the following command by keyboard: primos entrada.txt -t -n 5

What they are saying to you there is that the program is running with certain arguments. That is, your program will be called primos and, instead of executing it simply as .\primos will be executed with certain parameters .\primos entrada.txt [-t -n 5] . The parameters that I have put in brackets understand that they are optional, that is, if the program is started without -t then everything will be executed in a single thread.

Well, to read these input parameters you have to modify your main

int main(int argc, char** argv)
{
}

where:

  • argc : indicates the number of input parameters (minimum 1).
  • argv : an array with argc parameters (in text format). Remember that argv[0] always indicates the name of the executable.

So, you could have the following:

  • argv[0] : name of the executable
  • argv[1] == "-t"
  • argv[2] == "-n"
  • argv[3] == "5" - > note that 5 is in characters, not numerical digits

I do not go deeper into the topic because I understand that working on reading those parameters is a fundamental part of the exercise.

    
answered by 04.11.2017 в 18:23