Code:
#include <stdio.h>
#include <string.h>
char surname[20] = {0}, postcode[7] = {0}, rooms[20] = {0}, price[6] ={0};
int listchoice, house, cnt, totalcnt;
FILE *fp, *outfile;
struct house{
char surname[20];
char postcode[7];
char rooms[20];
char price[6];
} house_details;
int list(struct house house_details, int, char *);
void addhouse(struct house house_details, int, char *);
void deletehouse(struct house house_details, int, char *);
void view(struct house house_details, int, char *);
void save(struct house house_details, char *);
int main (void)
{
char filename[25];
printf("\n\nEnter file name to open: ");
scanf("%s", &filename);
fp=fopen(filename,"r");
if (fp == NULL){printf("\nUnable to open file\n");return(0);}
while(fscanf(fp, "%s %s %s %s", &house_details[cnt].surname, &house_details[cnt].postcode, &house_details[cnt].rooms, &house_details[cnt].price) != EOF) ++cnt;
totalcnt = cnt;
list(house_details, totalcnt, filename);
return 0;
}
int list(struct house house_details, int totalcnt, char *filename){
printf("\nHouse list:\n1) Add new record\n2) Delete record\n3) View phone book\n6);
scanf("%d", &listchoice);
switch (listchoice)
{
case 1: addrecord(house_details, totalcnt, filename); break;
case 2: deleterecord(house_details, totalcnt, filename); break;
case 3: view(house_details, totalcnt, filename); break;
case 4: save(house_details, filename); return 0;
}
save(house_details, filename);
return 0;
}
void addrecord(struct house house_details, int totalcnt, char *filename){
printf("\nAdd Record\n");
printf("Owner Surname: ");
scanf("%s", house_details[totalcnt+1].surname);
printf("\nPostcode: ");
scanf("%s", house_details[totalcnt+1].postcode);
printf("\nNumber of rooms: ");
scanf("%s", house_details[totalcnt+1].rooms);
printf("\nPrice: ");
scanf("%s", house_details[totalcnt+1].price);
printf("\nHouse added");
++totalcnt;
list(house_details, totalcnt, filename);
}
void deleterecord(struct house house_details, int totalcnt, char *filename){
char deletesurname[20], yn;
printf("\nDelete Record\n");
printf("Surname: ");
scanf("%s", &deletesurname);
printf("\nDelete file? (y/n): ");
scanf("%s", &yn);
if (yn == 'y'){
}
list(house_details, totalcnt, filename);
}
void view(struct house house_details, int totalcnt, char *filename){
int i;
printf("\nView Records\n");
for (i=0;i<totalcnt;i++)
{
printf("\n%s %s %s %s", house_details[i].surname, house_details[i].postcode, house_details[i].rooms, house_details[i].price);
}
list(house_details, totalcnt, filename);
}
void save(struct house house_details, char *filename){
outfile = fopen(filename, "w");
fprintf(outfile, "%s", house_details);
}