The routine below is called twice for two separate lists. The first time I call it, it reads and loads data from 100 lines in 5 files.
The second time I call it, it only reads 6 lines from a few files before messing up on reading the date and crashing. When I print out the results of fgets, every line is properly printed.
ideas?
The second time I call it, it only reads 6 lines from a few files before messing up on reading the date and crashing. When I print out the results of fgets, every line is properly printed.
ideas?
Code:
void load_data(char *dir_path, char* list, float **array1, float **array2, char **date_list)
{
FILE *FILELIST, *SEC;
char *name, *full_path, *line_of_data, *close_s, *open_s, **symbol_list;
int day, i, symbol_number = 0;
float close = 0, open = 0;
close_s = (char *) calloc (30, sizeof(char));
open_s = (char *) calloc (30, sizeof(char));
name = (char *) calloc (100, sizeof(char));
full_path = (char *) calloc (150, sizeof(char));
line_of_data = (char *) calloc (150, sizeof(char));
symbol_list = (char**) calloc(number_of_indices, sizeof(char*));
for (i = 0; i <number_of_indices; i++)
symbol_list[i] = (char *)calloc(10, sizeof(char));
if( (FILELIST = fopen(list,"r")) == NULL)
{
printf( "couldn't open file list\n");
}
while (fscanf(FILELIST, "%s\n", name) != EOF && symbol_number < number_of_indices)
{
{
strcpy(full_path, dir_path); strcat(full_path, name);
if ( (SEC = fopen(full_path, "r")) == NULL)
printf( "couldn't open index file %s\n", full_path);
else
printf( "opened index file %s\n", full_path);
day = 0;
while ( (fgets(line_of_data, 150, SEC)) && day < back_test_period)
{
// printf("%s\n", line_of_data);
close_s = strtok (line_of_data,","); //symbol
printf("%s\n",close_s);
if ( day == 0)
strcpy(symbol_list[symbol_number],close_s);
close_s = strtok (NULL, ",");
printf("%s\n",close_s);
close_s = strtok (NULL, ",");// date
printf("%s\n",close_s);
// printf("%s %s\n",close_s,date_list[day]);
if (strcmp(close_s,date_list[day]) == 0)
{
if (strcmp(close_s, "<TICKER>") != 0)
{
close_s = strtok (NULL, ","); ///00000
printf("%s\n",close_s);
open_s = strtok (NULL, ",");
printf("%s\n",open_s);
for (i = 0; i < 3; i++)
{
close_s = strtok (NULL, ",");
printf("%s\n",close_s);
}
}
close = (float)atof(close_s);
open = (float)atof(open_s);
// printf("%s %f\n", close_s, close);
array1[symbol_number][day] =open;
array2[symbol_number][day] =close;
}
// printf("%d\n", day);
day++;
}
fclose(SEC);
}
symbol_number++;
}
fclose(FILELIST);
return ;
}