Question: File IO: You need to include the struct Person, createPerson, and printPerson from 2). (Code 2) is at very bottom) a) Write a function that
File IO: You need to include the struct Person, createPerson, and printPerson from 2). (Code "2)" is at very bottom) a) Write a function that opens a file named test and prints some random text to the file. (not graded) b) Write a program that opens a file name test and prints the content of the file to screen. (not graded) III) Write a function void savePerson(char fileName[], Person p)that opens a file named fileName and prints the person to the file, as follows:
Name age score // Ex. Rickard 31 100
c) Write a function Person loadPerson(char fileName[]) that reads the file created by savePerson and returns a Person with the attributes in the file.
The function prototypes and a sample main are provided below:
Person createPerson(char name[],int age,double score); //from 2) void printPerson(Person p); //from 2) void savePerson(char fileName[], Person p); Person loadPerson(char fileName[]);
void hw5_3(){ Person p = createPerson("Rickard",31,100); char file[] = "testPerson"; savePerson(file,p); //check file manually Person p2 = loadPerson(file); printPerson(p2); };
CODE 2:
#include
#include
typedef struct
{
char name[20];
int age;
double score;
}Person;
Person createPerson(char name[],int age,double score);
void printPerson(Person p);
void changeAge(Person * p,int newAge);
void printPeople(Person people[],int length);
int main()
{
Person p = createPerson("Rickard",31,100);
printPerson(p);
changeAge(&p,30);
printPerson(p);
Person people[3];
people[0] = createPerson("John",22,83);
people[1] = createPerson("Donnie",24,63);
people[2] = createPerson("Marry",20,81);
printPeople(people,3);
return 0;
}
Person createPerson(char name[],int age,double score)
{
Person p;
strcpy(p.name,name);
p.age=age;
p.score=score;
return p;
}
void printPerson(Person p)
{
printf("Name :%s ",p.name);
printf("Age :%d ",p.age);
printf("Score :%.2lf ",p.score);
}
void changeAge(Person * p,int newAge)
{
p->age=newAge;
}
void printPeople(Person people[],int length)
{
int i;
for(i=0;i { printf("____ Person#%d ____ ",i+1); printPerson(people[i]); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
