I have to build a function that reads backer
and reward
information (structs) from files. If the files do not exist, notify the user, create new files and display the main menu. Once a file is open, data is read into an array of structs by dynamically allocating sufficient memory for each entry read from the file. Here is how far I have gotten. I'm not sure how to approach the array of structs or to allocate the memory correctly. It's a topic I'm not that strong on.
struct backer_info {
int backer_ID;
char *backer_name[40];
char *email[40];
char *country[20];
}backer;
struct reward_info {
int reward_number;
int backer_ID;
float price;
int num_drones;
int priority;
}reward;
void load_data(){
int i;
int j;
FILE *backers;
FILE *rewards;
struct backer_info backer = {0, "", "", ""};
if ((backers = fopen("backers.txt", "rb")) == NULL) {
printf("File not found \n New backers file created \n");
}
else{
for (i = 1; i <= !feof; i++) {
malloc(sizeof(sizeof(struct backer_info)));
fread(&backer, sizeof(struct backer_info), 1, backers);
}
fclose(backers);
}
struct reward_info reward = {0, 0, 0, 0, 0};
if ((rewards = fopen("rewards.txt", "rb")) == NULL) {
printf("File not found \n, New rewards file created \n");
}
else{
for (j = 1; j <= !feof; j++) {
malloc(sizeof(struct reward_info));
fread(&reward, sizeof(struct reward_info), 1, rewards);
}
fclose(rewards);
}
/* Use the allocated space */
}