Question: Hello, I am in a C PROGRAMMING class and need help finishing this code. I must finish case three and the last function call. The
Hello, I am in a C PROGRAMMING class and need help finishing this code. I must finish case three and the last function call. The program is based off FILE I/O it creates a file called input.txt and can create a person, print the file, search for a person, or exit. I am stuck with search for a person. The function call and prototype must not change. The program runs and compiles correctly as is but is missing the earlier said function.
Thank you
#include
void addPerson(); void printFile(); bool readFileRecord(char *name);
struct person{ char name[100]; char number[100]; char address[100]; };
int main(){
int choice; char name[100];
printf("1. Add person to file 2. Print person file 3. Search for person 4. Exit "); scanf(" %d",&choice); getchar();
while(choice!=4){
switch(choice){
case 1:
addPerson(); break;
case 2:
printFile(); break;
case 3:
//case three must search for a person in a file and tell if the person exists
break;
case 4:
break;
default:
printf("Enter a valid option! "); break;
}
choice = 0; printf("1. Add person to file 2. Print person file 3. Search for person 4. Exit "); scanf("%d",&choice); getchar();
}
return 0;
}
void addPerson(){
struct person *p = malloc(sizeof(struct person)); FILE *fp;
printf("Enter value for name: "); fgets(p->name,99,stdin); printf("Enter value for phone: "); fgets(p->number,99,stdin); printf("Enter value for address: "); fgets(p->address,99,stdin);
fp = fopen("input.txt","ab");
if(fp!=NULL){ fwrite(p,sizeof(struct person),1,fp); fclose(fp); } }
void printFile(){
struct person *p = malloc(sizeof(struct person)); FILE *fp;
if(access("input.txt",F_OK)!=-1){ fp = fopen("input.txt","rb");
while(fread(p,sizeof(struct person),1,fp)){ printf("%s",p->name); printf("%s",p->number); printf("%s",p->address); }
fclose(fp);
}
else{ printf("File does not exist! "); }
}
bool readFileRecord(char *name){
//this function must be completed //if the search person exists in the file it must //return a bool true and print said name //if not return bool false
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
