Where it says "HERE" are two print statements. From the first one I learn that I continue to load the data in the close_array with out apparent problems. From the second one I see that first the data in close_array[0] are changed to zero then to nonsense; I eventually get an EXC_BAD_ACCESS error. All suggestions appreciated.
}
Code:
dir = (char *)calloc(200, sizeof(char));
list = (char *)calloc(200, sizeof(char));
data_days = (int *) calloc (number_of_indices, sizeof(int));
/* date_list allocated in another routine
dates = (char**) calloc(days_of_data, sizeof(char*));
for (i = 0; i < days_of_data; i++)
dates[i] = (char *)calloc(12, sizeof(char));
*/
vol_close_array = (float **)calloc(number_of_vols, sizeof(float*));
for (i = 0; i < number_of_vols; i++)
vol_close_array[i] = (float *)calloc(days_of_data, sizeof(float));
vol_list = load_vols(dir, list, vol_close_array, date_list, data_days, &min);
char** load_vols(char *dir_path, char* list, float **close_array, char **date_list, int *data_days, int *min)
{
FILE *FILELIST, *newfile;;
char *close_s, **symbol_list;
int day = 0, i, symbol_number = 0;
char *oldname,*newname, *firstline, *date, *junk;
oldname = (char *)calloc(100, sizeof(char));
newname = (char *)calloc(100, sizeof(char));
firstline = (char *)calloc(75, sizeof(char));
date = (char *)calloc(14, sizeof(char));
junk = (char *)calloc(14, sizeof(char));
close_s = (char *) calloc (10, sizeof(char));
symbol_list = (char**) calloc(number_of_vols, sizeof(char*));
for (i = 0; i <number_of_vols; i++)
symbol_list[i] = (char *)calloc(10, sizeof(char));
*min = 10000;
if( (FILELIST = fopen(list,"r")) == NULL)
{
printf( "couldn't open file list\n");
}
while((fscanf(FILELIST,"%s\n", oldname) != EOF))
{
strcpy(newname, vol_file_directory);
strcat(newname, oldname);
if( (newfile = fopen(newname,"r")) == NULL)
printf("couldn't open file %s\n", newname);
do
{
fgets(firstline, 300, newfile);
date = strtok (firstline,",");
}
while ( strcmp(date, "04/25/2013"));
junk = strtok (NULL, ","); //time
junk = strtok (NULL, ",");//open
junk = strtok (NULL, ","); //high
junk = strtok (NULL, ","); //low
close_s = strtok (NULL, ","); //close
day = 0;
close_array[symbol_number][days_of_data - day - 1] = (float)atof(close_s);
printf("%d %d %s %f\n",symbol_number, day, date, close_array[symbol_number][days_of_data - day - 1]);
day ++;
while ( fgets(firstline, 75, newfile) && day < days_of_data)
{
date = strtok (firstline,",");
junk = strtok (NULL, ","); //time
junk = strtok (NULL, ",");//open
junk = strtok (NULL, ","); //high
junk = strtok (NULL, ","); //low
close_s = strtok (NULL, ","); //close
if (day == 1)
strcpy(symbol_list[symbol_number],oldname);
close_array[symbol_number][days_of_data - day - 1] = (float)atof(close_s);
//HERE
printf("%d %s %d %s %f\n",symbol_number,symbol_list[symbol_number], day, date, close_array[symbol_number][days_of_data - day - 1]);
printf("%d %s %d %f\n",symbol_number, symbol_list[0], day, close_array[0][day]);
day++;
}
data_days[symbol_number] = day;
if (data_days[symbol_number] < *min)
*min = data_days[symbol_number] ;
symbol_number++;
fclose(newfile);
}
fclose(FILELIST);
return (symbol_list);