Create a text file that stores a First name, a last name, and an age, one record
ID: 3927542 • Letter: C
Question
Create a text file that stores a First name, a last name, and an age, one record per line.
The C program will read a line of text from this file and then create a structure (what other languages would call a record) containing that data. Then write the object in a new binary file. The binary file that you have created should contain a series of records, each record containing a first name, last name and an age.
The program should then close the file and re-open it for input, read the file record by record and display on the screen, one record per line.
Explanation / Answer
#include<stdio.h>
struct student
{
int age;
char fname[20],fname[20];
float fee;
};
void main()
{
FILE *fp;
char ch;
struct student s;
clrscr();
fp=fopen("s5.bin","wb");
while(1)
{
printf(" Enter student details:");
scanf("%s%s%d",s.fname,s.lname,&s.age);
fwrite(&s,sizeof(s),1,fp);
fflush(stdin);
printf("Another record?[y/n]:");
ch=getche();
if(ch!='y')
break;
}
fclose(fp);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.