I am trying to read in data from a CSV file into an array of structs. The program has counted how many structs it will need and allocated enough memory for them but my attempt to scan in the data into the array isn't working correctly. This is the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct{
char *date;
double open;
double high;
double low;
double close;
double volume;
double adjclose;
} DATA;
DATA *table, *highest_table;
int main(int argc, char *argv[]){
int size_counter = 0;
DATA *table = (DATA *)malloc(size_counter*sizeof(DATA));
DATA *highest_table = (DATA *)malloc(sizeof(DATA));
FILE *file_input;
file_input = fopen(argv[1], "r");
//This counts to see how many lines are in the file to dedicate enough memory
char buffer;
for(buffer = getc(file_input); buffer != EOF; buffer = getc(file_input)){
if(buffer == '\n')
size_counter++;
}
size_counter = size_counter - 1;
//Inputting the data from line into a series of dynamically allocated structers
for(int i = 0; i < size_counter; i++){
fscanf(file_input, "%c %lf %lf %lf %lf %lf %lf", &table[i].date, &table[i].open, &table[i].high, &table[i].low, &table[i].close, &table[i].volume, &table[i].adjclose);
}
printf("Date: %c\n", table[0].date);
printf("Open: %f\n", table[0].open);
printf("High: %f\n", table[0].high);
printf("Low: %f\n", table[0].low);
printf("Close: %f\n", table[0].close);
printf("Volume: %f\n", table[0].volume);
printf("Adj. Close: %f\n", table[0].adjclose);
return 0;
}
This is a little bit of the input file. However there is the first line that is completely irrelavent but it has to be ignored(No I cannot remove the first line because this is an assignment.)
Date,Open,High,Low,Close,Volume,Adj. Close
17-Mar-06,11294.94,11294.94,11253.23,11279.65,2549619968,11279.65
16-Mar-06,11210.97,11324.80,11176.07,11253.24,2292179968,11253.24
15-Mar-06,11149.76,11258.28,11097.23,11209.77,2292999936,11209.77