Write a C program to write all the members of an array of structures to a file using fwrite(). Read the array from the file and display on the screen.
#include<stdio.h>
#include<conio.h>
struct family
{
char name[30];
int age;
};
void main()
{
struct family a[10],b[10];
int i;
FILE *fptr;
fptr=fopen("D:\\sentence.txt","wb");
printf("Enter data for 10 members: ");
for(i=0;i<10;i++)
{
fflush(stdin);
printf("\nFor member %d",i+1);
printf("\nEnter name: ");
gets(a[i].name);
printf("Enter age: ");
scanf("%d",&a[i].age);
}
fwrite(a,sizeof(a),1,fptr);
fclose(fptr);
fptr=fopen("D:\\sentence.txt","rb");
fread(b,sizeof(b),1,fptr);
printf("\n\n\nReading from file......");
for(i=0;i<10;++i)
{
printf("\nName: %s\tAge: %d",b[i].name,b[i].age);
}
fclose(fptr);
getch();
No comments:
Post a Comment