Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello, I am in a C PROGRAMMING class and need help finishing this code. I must f

ID: 3819187 • Letter: H

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 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 <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdbool.h>

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

}

Explanation / Answer

// C code
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdbool.h>
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
   printf(" Enter Name to search in file ");
               scanf("%s", name);
               bool found = readFileRecord(name);
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","w");
if(fp!=NULL){
fprintf(fp, "%s %s %s ", p->name, p->number, p->address);
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
// }

bool readFileRecord(char *name)
{
FILE *fp;
char *ch;
struct person *p = malloc(sizeof(struct person));
int i, flag = 1;
fp= fopen("input.txt","r");
while(EOF != fscanf(fp,"%s %s %s",p->name, p->number, p->address))
{ // read data
for(i=0; p->name[i]!=''; i++)
{ // compare with input array
if(p->name[i] != name[i])
{ // if not equal then set the flajg to 0.
flag = 0;
break;
}
}
if(flag == 1)
{ // if flag remains 1 after comparing then name are equal.
printf(" Name : %s", p->name); // print data.
printf(" Phone : %s", p->number);
printf(" Address : %s ", p->address);
}
flag = 1;
}

if(flag == 1)
return true;
else
return false;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote