Quero separar cada linha de um arquivo em 2 vetores: v[i].date and v[i].value. Porém, quando rodo o código nenhum valor que imprimo está correto, e os outputs sao valores aleatórios. Há algo que eu deveria mudar?
Input
02/20/18,11403.7
02/19/18,11225.3
02/18/18,10551.8
02/17/18,11112.7
02/16/18,10233.9
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
typedef struct
{
char *date;
long int *value;
}vetor;
int main(int argc,char **argv)
{
FILE *btc;
if((btc=fopen("BTC.csv", "r")) == NULL )
{
printf("not found btc\n");
exit(-1);
}
long int a=0;
char linha[256];
char *token = NULL;
while (fgets(linha, sizeof(linha), btc) != 0)
{
a++;
}
vetor *v;
v=(vetor*)malloc(a*sizeof(vetor));
char linha2[256];
while (fgets(linha2, sizeof(linha2), btc) != 0)
{
for(int i=0;i<a;i++)
{
fscanf(btc,"%[^,]s",v[i].date);
fscanf(btc,"%d[^,]",&v[i].value);
fseek(btc, +1, SEEK_CUR);
}
}
fclose(btc);
return 0;
}
rewind(btc)
para voltar o arquivo ao estado inicial antes do segundo while.