Hello, can someone help Take a look at the attached file “ structConversion.c ”.
ID: 3725078 • Letter: H
Question
Hello, can someone help
Take a look at the attached file “structConversion.c”.
Use the following struct template named “Person” in the program. Modify the following two functions as follows.
void printData(struct person x); struct person readData(); Replace gets with fgets.
You can use any additional helper functions. Submit the complete file as “structConversionLab5.c” file.
here is the code "
#include <stdio.h>
#include <stdlib.h>
void readData(char name[], char ssn[], int* age, float *height, float *weight){
char buff[BUFSIZ];
gets(name);
gets(ssn);
*age = atoi(gets(buff));
*height = atof(gets(buff));
*weight = atof(gets(buff));
}
void printData(char name[], char ssn[], int age, float height, float weight){
printf("%s %s Age = %d Height(cm) = %g Weight(kg) = %g ", name, ssn, age, height, weight);
}
int main(){
char name[BUFSIZ];
char ssn[BUFSIZ];
int age;
float height;
float weight;
printf("Reading data ...... ");
readData(name, ssn, &age, &height, &weight);
printf(" ");
printf("Printing data .... ");
printData(name, ssn, age, height, weight);
return 0;
}"
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
void readData(char name[], char ssn[], int* age, float *height, float *weight, FILE *fp){
char buff[BUFSIZ];
fgets(name,60,fp);
fgets(ssn,60,fp);
*age = atoi(fgets(buff,60,fp));
*height = atof(fgets(buff,60,fp));
*weight = atof(fgets(buff,60,fp));
}
void printData(char name[], char ssn[], int age, float height, float weight){
printf("%s %s Age = %d Height(cm) = %g Weight(kg) = %g ", name, ssn, age, height, weight);
}
int main(){
char name[BUFSIZ];
char ssn[BUFSIZ];
int age;
float height;
float weight;
FILE *fp;
char str[60];
fp=fopen("file.txt","r");
if(fp==NULL){
perror("Error opening file");
return(-1);
}
printf("Reading data ...... ");
readData(name, ssn, &age, &height, &weight, fp);
printf(" ");
printf("Printing data .... ");
printData(name, ssn, age, height, weight);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.