I want to separate each line of a file into 2 vectors: v [i] .date and v [i] .value. However, when I run the code, no value I print is correct, and the outputs are random values. Is there something I should change?
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;
}