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

File IO (person.c) In this program you will demonstrate how to read and write da

ID: 3814194 • Letter: F

Question

File IO (person.c) In this program you will demonstrate how to read and write data from and to a file in a person record simulator. A person record should have three attributes: name, phone number, and address. You may use a structure if you wish to define a person in this program but regardless of implementation you should keep track of a name, phone, and address field for each person in the file ‘database’. You may assume that each field will be no more than 100 characters. Blank spaces are allowed in all input fields. There will only be three functions in the program: one to add a person to the file, one to print the file, and one to search the file for a particular person’s name. You will use a text file named input.txt. You need not turn it it but should use it for testing purposes. In the grader a blank text file of the same name will be provided and written to. Your program needs to expect the file to have that name or it will not be graded properly. The exact signatures of the functions are outlined below. Implement these functions: 1 void addPerson(); void printFile(); bool readFileRecord(char *name); The structure of the data storage is left to the programmer but it is suggested to use a space or comma delineated structure in the text file. In other words, name, phone and address data should be separated by comma or white space. addPerson() function: it should open in the append mode of a text file named “input.txt”. Then it should prompt the user for the following three input data: name, phone number, and address. Name and address could include white spaces. Also these three input data should be separated either by comma or white space. Finally the function adds a single record to the file and then close the file. printFile() function: it should print the contents of the file to the standard output. Specifically, it should print name, phone, and address for each person with some blank space or other way to tell when one record ends and the next begins. readFileRecord(char *name) function: it implements a basic search function. It will get the “name” data from the argument and search through the file for this name. If the name exists in the file then it will return a boolean value of “true”, otherwise it will return “false”. Specific assumptions for this program: You should not rely on the fact of the contents of the text file existing between executions of the program. You should not count on the existence of the file in read operations and should check to make sure that the file exists before reading from it. Writing in append mode automatically creates the file so the check is not needed here. The web grader will be set up to delete your file on exit, you need not program your program to delete the file. Sample Output: Note: To save space the add person function is only shown once, it is called again for the second data entry and is just omitted here. 2 1: Add person to file 2: Print person file 3: Search for person 4: EXIT 1 Enter value for name: Kyle Willett Enter value for phone: 123456789 Enter value for address: Lincoln NE ....Second add person call omitted... 1: Add person to file 2: Print person file 3: Search for person 4: EXIT 2 Kyle Willett 123456789 Lincoln NE Joe 987654321 test test 1: Add person to file 2: Print person file 3: Search for person 4: EXIT 3 Enter Name to Search for: Kyle Willett Kyle Willett was found in the file 1: Add person to file 2: Print person file 3: Search for person 4: EXIT

Explanation / Answer

// C code person.c
#include <stdio.h>
#include <stdlib.h>
struct person{
char name[100];
char phone[12];
char address[100];
};
typedef struct person person;


void addPerson(){
person p;
FILE *fp ;
fp = fopen("input.txt", "a"); // open the file
getchar();
printf(" Enter value for name :");
scanf("%[^ ]s", p.name);
getchar();
printf(" Enter value for phone :");
scanf("%[^ ]s", p.phone);
getchar();
printf(" Enter value for address :");
scanf("%[^ ]s", p.address);
fprintf(fp, "%s %s %s ", p.name, p.phone, p.address); // print person name phone and address in file
fclose(fp); // close file.
printf(" Person has been added");

}
void printFile(){
FILE *fp;
char *ch;
person p;
fp= fopen("input.txt","r");
while(EOF != fscanf(fp,"%[^ ] %[^ ] %[^ ] ",p.name, p.phone, p.address)){ // read until EOF is discovered.
printf(" Name : %s", p.name); //print data .
printf(" Phone : %s", p.phone);
printf(" Address : %s ", p.address);
}

}
void readFileRecord(char *name){
FILE *fp;
char *ch;
person p;
int i, flag = 1;
fp= fopen("input.txt","r");
while(EOF != fscanf(fp,"%s %s %s",p.name, p.phone, 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 flag 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.phone);
printf(" Address : %s ", p.address);
}
flag = 1;
}
}
int main()
{
int flag = 1, choice;
char name[100];
while(flag){

printf(" 1. Add person to file");
printf(" 2. Print person file");
printf(" 3. Search for a person");
printf(" 4. Exit");
printf(" Enter a choice :"); // take user's choice
scanf("%d", &choice);
switch(choice){ // call function accordingly.
case 1 : addPerson(); break;
case 2 : printFile(); break;
case 3 : printf(" Enter Name to search in file ");
scanf("%s", name);
readFileRecord(name);
break;
case 4 : flag = 0; break;
default : printf("Please enter valid choice");
}

}
return 0;
}

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