Read a comma separated string

My reading has been set to the comma, but it is reading every field until the end of the line when it reads the string. The code is read correctly, but only in the code.

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

struct setorEletronicos {
    int codigo;
    char descricao[100];
    int altura;
    int largura;
    int profundidade;
    float preco;
    int estoque;
    char cor[100];
} ;


int main() {
    int i = 0;
    FILE *arquivo;
    struct setorEletronicos eletro[50];

    if ((arquivo = fopen("eletro.txt", "r")) == NULL) {
        printf("Erro ao abrir o arquivo.\n");
    } else {
        while (!feof(arquivo)) {

            fscanf(arquivo, "%d[^,]", &eletro[i].codigo);
            fscanf(arquivo, "%s[^,]", eletro[i].descricao);
            fscanf(arquivo, "%d[^,]", &eletro[i].altura);
            fscanf(arquivo, "%d[^,]", &eletro[i].largura);
            fscanf(arquivo, "%d[^,]", &eletro[i].profundidade);
            fscanf(arquivo, "%f[^,]", &eletro[i].preco);
            fscanf(arquivo, "%d[^,]", &eletro[i].estoque);
            fscanf(arquivo, "%s[^\n]", eletro[i].cor);
            i++;

        }
        fclose(arquivo);
    }

    int aux;
    for (aux = 0; aux < i; aux++) {
        printf("%d \n", eletro[aux].codigo);
        printf("%s \n\n", eletro[aux].descricao);
    }
    return (0);


}

Electro file.txt

100,Geladeira,180,90,89,1200.00,4,branca
101,Geladeira,180,90,89,1200.00,2,prata
102,Aspirador,30,50,60,149.99,5,vermelho
103,Aspirador,30,50,60,149.99,3,azul
104,Ar Condicionado 18000BTU,50,100,40,2967.00,13,branco
105,Ar Condicionado 9000BTU,50,80,40,1299.00,10,branco
106,TV LCD 42,80,110,15,2500.00,25,preto
105,Forno Eletrico com Microondas,39.2,52.7,0.48,1688.39,7,prata
106,Lavadora de Roupas 1Kg,46,32,32.9,435.00,1,branco
107,Lavadora de Roupas 10Kg,146,70,72.5,959.00,2,branco
108,Radio CD MP3,12.2,34.1,23.6,199.99,100,preto
109,Antena de TV Externa,16.2,118.5,6.5,199.00,5,cinza
110,TV 29 Slim,70,85,65,599.99,3,preta
Author: pmargreff, 2014-12-02

2 answers

A visible error in the code is with reading the description:

fscanf(arquivo, "%s[^,]", eletro[i].descricao[50]);

What you should pass to fscanf is a pointer to where to store the read string. But as an argument you passed a fifty-first element of an array. Simply pass the array itself.

Must also omit the s from the specifier. Otherwise you will be reading a string (the whole line) followed by the characters [^,]. Use, like this:

fscanf(arquivo, "%[^,]", eletro[i].descricao);
 3
Author: Guilherme Bernal, 2014-12-02 22:51:15

The problem as the friend talked about is that the pointer would stop at the comma and lock there until it would go to the next line with the \n and it would all happen again. I solved this with fseek, follows resolution code.

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

struct setorEletronicos {
    int codigo;
    char descricao[100];
    int altura;
    int largura;
    int profundidade;
    float preco;
    int estoque;
    char cor[100];
};

struct setorEletronicos eletro[50]; //declara o struct


int le_arquivo();
int main() {
    int cont = 0; // número de aparelhos lido 

    cont = le_arquivo(); 

    return (0);
}

/*
 * função não recebe argumentos
 * le os arquivos e salva na struct
 * para não ler a vírgula pula um 1 byte com f_seek
 * toda vez que chega na mesma, tem como retorno o
 * número de aparelhos lido
 */
int le_arquivo(){
    int i = 0;
    FILE *arquivo;
    if ((arquivo = fopen("eletro.txt", "r")) == NULL) {
        printf("Erro ao abrir o arquivo.\n");
    } else {
        while (!feof(arquivo)) {

            fscanf(arquivo, "%d[^,]", &eletro[i].codigo);
            fseek(arquivo, +1, SEEK_CUR);
            fscanf(arquivo, "%[^,]*c", eletro[i].descricao);
            fseek(arquivo, +1, SEEK_CUR);
            fscanf(arquivo, "%d[^,]", &eletro[i].altura);
            fseek(arquivo, +1, SEEK_CUR);
            fscanf(arquivo, "%d[^,]", &eletro[i].largura);
            fseek(arquivo,+1,SEEK_CUR);
            fscanf(arquivo, "%d[^,]", &eletro[i].profundidade);
            fseek(arquivo,+1,SEEK_CUR);
            fscanf(arquivo, "%f[^,]", &eletro[i].preco);
            fseek(arquivo,+1,SEEK_CUR);
            fscanf(arquivo, "%d[^,]", &eletro[i].estoque);
            fseek(arquivo,+1,SEEK_CUR);
            fscanf(arquivo, "%[^\n]*c", eletro[i].cor);
            i++;

        }
        i--;
        fclose(arquivo);
    }
    return i;
}
 1
Author: pmargreff, 2014-12-02 23:40:12