0

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

1 Answer 1

2

A couple of problems at first glance:

  1. Your first loop to calculate the file size positions the read stream past the end of the file. Use rewind or fseek to put it back.

  2. Your data is comma separated; your fscanf call is using whitespace. Either fix that format, or maybe better, use fgets to get a whole line and then use strtok to parse.

Sign up to request clarification or add additional context in comments.

5 Comments

How can I use fgets and strtok to pick out each individual data type and read it in correctly?
You know the type based on which field you're parsing; just convert them (strtod, probably?) as appropriate.
And besides: there is no reason to allocate memory before you know the actual size.
What about skiping the first line of the file with the labels of the data types? What can I do for that since I am not allowed to change that
fgets and discard the first line before proceeding with your data-reading loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.