C separating string with commas into vectors

0

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;
}
    
asked by anonymous 19.03.2018 / 17:48

1 answer

0

In your vector structure you define two pointers: a pointer to char (date) and another pointer to long int (value). To effectively store the read values you need to allocate the required space and have each of these pointers point to the respective memory space.

    
19.03.2018 / 18:58