The program I have reads the file, displays it accordingly, but then when trying to view the contents again, they are gone.
Structure:
typedef struct friends_contact {
char *First_Name;
char *Last_Name;
char *home;
char *cell;
} fr;
Main:
int main() {
fr friends[5];
char buffer[BUFFSIZE];
int counter = 0;
int i = 0;
menu(friends, &counter, i, buffer);
getch();
return 0;
}
If statement in the menu function used to open up the file:
if (user_entry1 == 1) {
printf("Please enter a file name");
scanf("%s", user_entry3);
read = fopen(user_entry3, "r");
}
If statement that allows the user to choose to look at the contacts of the address book.
if (user_entry == 4 ) {
print_contact(friends, counter, i, user_entry3);
if (user_entry1 == 1) {
file2(friends, counter, i, buffer, read);
}
}
Read function:
char file2(fr *friends, int *counter, int i, char buffer[], FILE *read) {
while (fscanf(read, "%s", buffer) != EOF) {
friends[i].First_Name = malloc(BUFFSIZE * strlen(buffer));
strcpy(friends[i].First_Name, buffer);
return printf("\n""%s ", friends[i].First_Name);
}
fclose(read);
}
If you need me to provide any more code. I’m trying to get it to read the content from the file into the contacts list so I can view, search, or delete them.
P.S. I understand some of the other code may not be perfect, but right now I’m just concerned about figuring this out :).
You have to reset your file pointer using
fseekto the beginning of the file again. Your read function should start like: