Friday, April 10, 2015

7. Write a program using C language that reads successive records from the new data file and display each record on the screen in an appropriate format.


#include<stdio.h>
#include<conio.h>
struct student
{
char name[30];
int roll,age;
}s;

void main()
{
FILE *ptr;
char choice;
clrscr();
ptr=fopen("record.txt","w");
do
{
printf("\n Input name:-");
gets(s.name);
printf("\n Input roll number and age:-");
scanf("%d%d",&s.roll,&s.age);
fwrite(&s,sizeof(s),1,ptr);
fflush(stdin);
printf("\n Any more record:-");
choice==getche();
}while(choice=='y'||choice=='Y');
fclose(ptr);
/*reading data form file and displaying its output on the screen*/
ptr=fopen("record.txt","r");
printf("\n Name \t Roll \t Age");
while(fread(&S,sizeof(s),1,ptr)==1)
printf("\n %s \t %d \t %d",s.name,s.roll,s.age);
fclose(ptr);
getch();
}

1 comment:

  1. question said to us read but why u write command used.
    i am waiting for your ans. plz try to be fast

    ReplyDelete