Problem reading blank line in a file in C

0

I'm having the problem that when the last line of my file is blank the program can not read and when it goes off normally.

Function that stores the data in the file

void exportCategoria_txt(Strc_Categoria Cat) {
    FILE * file = fopen("arquivos/fileCategorias.txt", "a");

    fprintf(file, "#, %s,", Cat.nome);
    fprintf(file, "%s,", Cat.descricao);
    fprintf(file, "%d,", Cat.codigo);
    fprintf(file, "%.2f \n", Cat.valor);
}

Function that read the data from the file and store it in memory

void importCategoria_txt() {
    FILE * file = fopen("arquivos/fileCategorias.txt", "r");
    if (file == NULL) {
        printf("Impossivel ler o arquivo. \n");
        menuInicial();
    }

    char line[256];
    char* token;

    Strc_Categoria cat;

    while (!feof(file)) {
        fgets(line, 256, file);

        if (line[0] == '#') {
            token = strtok(line, ",");

            while (token != NULL) {
                token = strtok(NULL, ",");
                strcpy(cat.nome, token);

                token = strtok(NULL, ",");
                strcpy(cat.descricao, token);

                token = strtok(NULL, ",");
                cat.codigo = atoi(token);

                token = strtok(NULL, ",");
                cat.valor = atof(token);

                alocarCategoria(&cat);
                token = strtok(NULL, ",");
            }
        }
    }

    fclose(file);
    free(file);
}

Is there any way to delete this blank line? If yes, how? Or do you have some way to dribble her while reading?

    
asked by anonymous 20.11.2018 / 23:19

0 answers