2
I need to read the data of an entry in the format:
100,Refrigerator,180,90,89,1200.00,4,white
After some researches, I found the Strtok function that separates the data between commas, and the code was as follows:
#include <string.h>
#include <stdio.h>
int main(){
const char s[2] = ",";
char *token;
char linha[90];
char *result;
FILE *arq;
if((arq = fopen("eletro.txt", "r")) == NULL){
printf("Erro ao abrir o arquivo.\n");
}
token = strtok(arq, s);
while (!feof(arq)){
result = fgets(linha, 90, arq);
if (result)
token = strtok(result, s);
while( token != NULL ){
printf( " %s\n", token );
token = strtok(NULL, s);
}
}
fclose(arq);
return(0);
}
The output of this file is exactly the way I wanted it, but my question is: how to save this data in the output format in their respective vectors, divided in the form eletro[i]. codigo, eletro[i]. name (...) until the end, and I need to read several lines of data? Or, there is a simpler way to do this?
You have to create a structure with the data you want and then use that structure in a list. Note that you must always allocate each structure (per line) you read and guards.
– Jorge B.
The structure is created, in this case. I just don’t understand in what way I will read, in what part of the code!
– Moni
Look at my answer.
– Jorge B.