If the while loop is not enter then all is well. If the while loop is entered then get_date is called a second time. No line is read in get_date and the program fails.
Code:
/
char* get_date(FILE *fp)
{
char *start_date, *line;
start_date = (char *)calloc(14, sizeof(char));
line = (char *)calloc(100, sizeof(char));
fgets(line,100,fp);
strtok(line,",");
strtok(NULL,",");
strcpy(start_date,strtok(NULL,","));
free(line);
return(start_date);
};
char * find_start_date( int i, int j, char **etf_list)
{
char *start_date1,*start_date2, *full_name, *line;
FILE *etf1,*etf2;
int comp, k;
start_date1 = (char *)calloc(14, sizeof(char));
start_date2 = (char *)calloc(14, sizeof(char));
line = (char *)calloc(100, sizeof(char));
full_name = (char *)calloc(100, sizeof(char));
strcpy(full_name,home);
strcat(full_name,etf_list[i]);
if ( (etf1 = fopen(full_name,"r") ) == NULL)
printf("did not open %s\n",full_name);
strcpy(full_name,home);
strcat(full_name,etf_list[j]);
if ((etf2 = fopen(full_name,"r")) == NULL)
printf("did not open %s\n",full_name);
// read first line in etf1
fgets(line,100,etf1);
start_date1 = get_date(etf1);
// read first line in etf2
fgets(line,100,etf2);
start_date2 = get_date(etf2);
fclose(etf1);
fclose(etf2);
while((comp = strcmp(start_date1,start_date2))!= 0)
if (comp > 0)
start_date2 = get_date(etf2);
else if (comp < 0)
start_date1 = get_date(etf1);
free(line);
free(full_name);
free(start_date2);
return(start_date1);
};
/[code]