Read and split C files

Asked

Viewed 64 times

1

I’m doing a project for the college, the code already reads the files and divides the lines taking into account the ";", how can I save these tokens in variables?

struct informacaoFicheiroInput{
    int id;
    int acompanhantes;
    char tipo[11];
    int entrada;
    int saida;
    int servico;
};

void lerFicheiroInput(){
    struct informacaoFicheiroInput informacao[20];

    FILE* file;
    file = fopen("input.txt","r");

    if(file == NULL){
        printf("Não foi possivel abrir o arquivo.\n");
    }

    char line[20], *token, dados[11][20];

    while(fgets(line, 100, file) != NULL){
        int count = 0;
        token = strtok(line,";");

        while(token != NULL) {
            dados[count++] = token;
            token = strtok(NULL, ";");
        }
    }
}

The input file is of the genus:

10 ; Visitante ; 10 ; 19 ; 2
2 ; 1 ; Funcionario ; 8 ; 0
3 ; 2 ; Diretor ; 12 ; 19
4 ; Visitante ; 8 ; 0 ; 3
  • 1

    You can do int inteiro = atoi(dados[x][y]); to obtain the integers, and strcpy(string, dados[x][y]); to copy strings. In the input file, delimiters were missing at the end of the line, 10; Visitante; 10; 19; 2;<quebra>

  • I’ll try, what does the atoi function? I can’t change the input file, how do I?

  • 1

    int atoi(const char* s) takes a string as parameter, tries to convert it to integer, and returns this integer value.

  • Thank you, I’m clear!

  • 1

    It may be necessary to include line breaking as a delimiter, in the strok: token = strtok(line, ";\n");

  • I could not use int integer = atoi(data[x][y]); to get integers, and strcpy(string, data[x][y]), you can enter this into code to see if it works?

  • What each element of a file line means?

  • If the type of employee is employee or director, the line will be of this genus 2 ; 1 ; Employee ; 8 ; 0, ie by order, id, companions, type, hours of entry, hours of exit If the type of employee is visitor, the line will be of type 4 ; Visitor ; 8 ; 0 ; 3, that is, by order, id, type, check-in time, check-in time, and employee id number you will visit

  • Are the spaces between data, shown in your file example, really there? Or were they included as readability?

  • The spaces before and after the ";" such as line breaks at the end of each sentence are there

Show 5 more comments

1 answer

1


Try this way:

void lerFicheiroInput(){
    struct informacaoFicheiroInput informacao[4];

    FILE* file;
    file = fopen("input.txt","r");

    if(file == NULL){
        printf("Não foi possivel abrir o arquivo.\n");
    }

    char line[100], *token, dados[5][20];
    int info = 0;

    while(fgets(line, 100, file) != NULL){
        int count = 0;
        token = strtok(line,";");

        while(token != NULL && count < 5) {
            dados[count++] = token;
            token = strtok(NULL, ";");
        }

        // Mete os dados lidos da info-esima linha
        // em informacao.
        if (strcmp(" Visitante ", dados[1]) == 0){
            informacao[info].id = atoi(dados[0]);
            strcpy(informacao[info].tipo, dados[1]);
            informacao[info].entrada = atoi(dados[2]);
            informacao[info].saida = atoi(dados[3]);
            informacao[info].servico = atoi(dados[4]);
        } else {
            informacao[info].id = atoi(dados[0]);
            informacao[info].acompanhantes = atoi(dados[1]);
            strcpy(informacao[info].tipo, dados[2]);
            informacao[info].entrada = atoi(dados[3]);
            informacao[info].saida = atoi(dados[4]);
        }
        ++info;
    }

    fclose(file);
}
  • I only have one problem, the given line[Count++] = token; is giving me error, is the following " assignment to Expression with array type"

  • Exchange the line of dados[count++] = token; for strcpy(dados[count++], token);, please. Thank you.

  • Also includes #include <string.h> at the top of the program.

  • 1

    So it already works well! Thank you very much

  • Please mark as reply if the code is useful to you.

  • 1

    It’s already done! I appreciate the help

Show 1 more comment

Browser other questions tagged

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