I am writing a program that reads from a file that contains a list of appointments and then put them into an dynamic array named day. I am having trouble figuring out what is exactly wrong with my program. It keeps on crashing before it finishes reading the file. I figured out that I could add the first day into my array, but I cannot add anymore than that. My guess would be that I did not allocate memory correctly, but I don't see anything wrong with it.
Here are my the functions and structures:
typedef struct {
int hour;
int minute;
} Time;
typedef struct {
Time startTime;
Time endTime;
char subject[20];
char location[20];
} Appointment;
typedef struct {
int day;
int month;
Appointment *appts[8];
int apptCount;
} Day;
int needMemory(int *daysize, int *allocate){
if (*daysize == *allocate) { //does day need more memory?
if (*allocate == 0) // if array is empty
*allocate = 30; // set initial size of array
else
*allocate *= 2; // else size is double
return TRUE;
}// if array needs more memory*/
return FALSE;
} //needMemory() does array need more memory
void addNewDay(Day **array, int daysize, int month, int day){
int sTh, eTh;
char AMPM[MAXSIZE];
array[daysize]->day = day;
array[daysize]->month = month;
strtok(NULL,"/,:\n");
array[daysize]->appts[0] = (Appointment *) malloc(sizeof(Appointment));
strcpy(array[daysize]->appts[0]->subject, strtok(NULL,"/,:\n"));
sTh = atoi(strtok(NULL,"/,:\n"));
array[daysize]->appts[0]->startTime.minute = atoi(strtok(NULL,"/,:\n"));
strcpy(AMPM, strtok(NULL,"/,:\n"));
array[daysize]->appts[0]->startTime.hour = changeMilitaryTime(sTh, AMPM);
eTh = atoi(strtok(NULL,"/,:\n"));
array[daysize]->appts[0]->endTime.minute = atoi(strtok(NULL,"/,:\n"));
strcpy(AMPM, strtok(NULL,"/,:\n"));
array[daysize]->appts[0]->endTime.hour = changeMilitaryTime(eTh, AMPM);
strcpy(array[daysize]->appts[0]->location, strtok(NULL, "/,:\n"));
array[daysize]->apptCount = 1;
} //addNewDay() adds appointment to a new day
void readFile(Day **array, int *daysize, int *allocate, FILE *inp){
char s[MAXSTRING];
int tempsize = *daysize, tempallo = *allocate, month, day, dayindex, i;
fgets(s,100, inp);
while ((fgets(s,80, inp)) != NULL){ //while not end of file
//for (i = 0; i<=1; ++i){
fgets(s,100, inp);
if ((needMemory(daysize, &tempallo)) == TRUE)
*array = (Day *) realloc(*array, tempallo * (sizeof(Day)));
month = atoi(strtok(s,"/,:\n"));
day = atoi(strtok(NULL,"/,:\n"));
dayindex = findApptDay(array, tempsize, month, day);
if (dayindex == -1) {//if day not found
addNewDay(array, tempsize, month, day); //add appointment to a new day
tempsize += 1;
//printf("%d",tempsize);
//printf("%s\n", "DEBUG");
} else {
//printf("%s", "DEBUG");
//printf("%d", dayindex);
addToday(array, tempsize, month, day, dayindex);
tempsize += 1;
// printf("%s", "DEBUG");
}
}//while not end of file
*daysize = tempsize;
*allocate = tempallo;
}// readFile()
int main(){
FILE *inp;
Day *day = NULL;
int choice, daysize = 0, allocate = 0, i;
inp = fopen("appts.csv","r");
readFile(&day, &daysize, &allocate, inp);
fclose(inp);
return 0;
}
There can only be one unique day in the array day and for each day, there can only be 8 appointments. The addToday function adds an appointment and it looks similar to the addNewday function so I didn't include it. Can anyone find out why I cannot add more than one day?