I need to read data from an entry in the format:
100, Refrigerator, 180,90,89,1200.00,4, white
After some search, I found the strtok function that separates the data between the commas, and the code looks like this:
#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 format of the output in their respective vectors, divided as electro [i] .codigo, electro [i] .name ...) to the end, and do I need to read multiple rows of data? Or is there a simpler way to do this?