When I compile this part of my program it will complie correctly, but when I run the program it always crashes. I have narrowed it down to the prinf statement right after I scan in the files. What am I doing wrong here?
(I din't include the rest of the program because my problem lie's within this area)
Code:
#include <stdio.h>
char lname[20], fname[15], phoneNo[12];
int menuchoice, record, cnt, totalcnt;
FILE *infile, *outfile;
struct record{
char lname[20];
char fname[15];
char phoneNo[12];
};
int menu(struct record phonebook, int);
void addrecord(struct record phonebook, int);
void deleterecord(struct record phonebook, int);
void editrecord(struct record phonebook, int);
void search(struct record phonebook, int);
void view(struct record phonebook, int);
void sort(struct record phonebook, int);
int main (void)
{
struct record phonebook;
char filename[20];
printf("\n\nEnter file name to open: ");
scanf("%s", &filename);
infile=fopen(filename,"r");
if (infile == NULL){printf("Unable to open file");return(0);}
while(fscanf(infile, "%s %s %s", &phonebook.lname[cnt], &phonebook.fname[cnt], &phonebook.phoneNo[cnt]) != EOF) cnt++;
printf("%s %s %s", phonebook.lname[0], phonebook.fname[0], phonebook.phoneNo[0]);
totalcnt = cnt;;
return 0;
}