I am trying to read the name, phone number and address from a file
here is the code but it doesn't work and i don't know where is the problem
and here is the contents of the file:
jack 01014 jgd
Moh 02925 Tyeu
when i run it on dev c++, a message appear saying that the prog has stopped working and no output appears
i want to put the name in user[n].name, the phone number in user[n].phoneNumber and the address in user[n].address
here is the code but it doesn't work and i don't know where is the problem
Code:
#include <stdio.h>
#include <stdlib.h>
struct PhoneBook{
char name[50];
int phoneNumber;
char address[50];
} ;
int read(struct PhoneBook user[], int n, FILE* fp);
void main()
{
//int i, j;
struct PhoneBook user[10];
FILE* fp = fopen("D:\\phoneBook.txt", "r");
int i = 0;
int counter;
while (!feof(fp)) {
read(user, i, fp);
i++;
}
counter = i;
}
int read(struct PhoneBook user[], int n, FILE* fp){
//char temp;
if(fp==NULL){
printf("Error\n");
return -1;
}
fscanf(fp,"%s %d %s\n", user[n].name,user[n].phoneNumber,
user[n].address);
}
jack 01014 jgd
Moh 02925 Tyeu
when i run it on dev c++, a message appear saying that the prog has stopped working and no output appears
i want to put the name in user[n].name, the phone number in user[n].phoneNumber and the address in user[n].address
Comment