In: Computer Science
You need a data structure that contains fields for name (char array), color (char array), age (int), and height (int). Name the struct Info when you define it.
Create a function named getUserInfo() that asks the user for name, color, age, and height and then returns a data structure containing that information. It should do the following:
What is your name? George
What is your favorite color? Green
What is your age? 111
What is your height in inches? 72
struct Info getUserInfo( void )
{
// Do stuff here
}
Create a function named saveUserInfo() that takes an Info data structure and a file object as a parameter and saves the structure to disk.
void saveUserInfo( struct Info info, FILE *fp )
{
// Do stuff here.
}
Assume that the user will enter five records.
In main begin by telling the user that they will enter five user information records. Then loop through five times calling the getUserInfo() function asking their user for the next information, then saving it to disk with the saveUserInfo() function.
Remember to open the file (with filename data.txt) before you begin the loop and close the file after you end the loop.
I have implemented C program which ask the 5 user details and store it into the "data.txt file".
Here, I create data structure "struct Info" which will store name, color, age and height in inches.
// create struct info which will store user info
struct Info{
// store user name
char name[50];
// store user favorite color
char color[50];
// store user age
int age;
// store user height
int height;
};
I also created two functions::
1> struct Info getUserInfo() :- This function ask all the information fromthe user and return struct variable to the main().
2> void saveUserInfo(struct Info info, FILE *fp) :- This function save the each user data into the "data.txt" file in while loop.
First, we have to open the file using FILE *fp; then open "data.txt" file using "fp" then close that file at the end of the file.
I have attached C program which will ask the user info and store to the "data.txt" file.
C Pogram:-
#include<stdio.h>
// create struct info which will store user info
struct Info{
// store user name
char name[50];
// store user favorite color
char color[50];
// store user age
int age;
// store user height
int height;
};
// This function ask the user info from the user
struct Info getUserInfo(){
// create struct variable which contain all the details of Info
struct Info temp;
// ask the name from the user
printf("What is your name? ");
scanf("%s", &(temp.name));
// ask the favorite color from the user
printf("What is your favorite color? ");
scanf("%s", &(temp.color));
// ask the age from the user
printf("What is your age? ");
scanf("%d",&(temp.age));
// ask the height in inches from the user
printf("What is your height in inches? ");
scanf("%d", &(temp.height));
printf("\n\n");
// return entered user detail
return temp;
}
// This function save the user data to the data.txt file
void saveUserInfo(struct Info info, FILE *fp){
fprintf(fp, "------------User Detail---------------\n");
// write user name into the data.txt
fprintf(fp, "Name : %s\n", info.name);
// write user favorite color into the data.txt
fprintf(fp, "Favorite color : %s\n", info.color);
// write user age into the data.txt
fprintf(fp, "Age : %d\n", info.age);
// write user height into the data.txt
fprintf(fp, "Height in inches : %d\n\n", info.height);
}
void main(){
// assume that user will enter five recoeds
int numberOfUser = 5;
int number = 1;
FILE *fp;
// open the "data.txt" file in write mode
fp = fopen("data.txt", "w");
// check whether file is opened or not
if(fp == NULL){
printf("File is not opened");
}else{
// run the loop untill the numbeOfUesr does not become 0
while(numberOfUser != 0){
printf("Enter user %d detail :\n", number);
// call the getUserInfo() which will ask the info from the user
struct Info userInfo = getUserInfo();
// call the saveUserInfo() which will save usr info to the data.txt file
saveUserInfo(userInfo, fp);
// decrement numberOFUser
numberOfUser--;
// increment number
number++;
}
// check whether data is wrrited successfully or not
if(fwrite != 0)
printf("contents to file written successfully !\n");
else
printf("error writing file !\n");
// close the file
fclose(fp);
}
}
Output:-
1> Program console:-
2> "data.txt" file:-
I hope you will understand how to create struct and store the user data into the text file using fprintf() function.
Do you feel needful and useful then please upvote me.
Thank you.