Tuesday, April 7, 2015

41. Write a program to open a new file and read roll no,name ,address and phone numbers of students until the user says "no",after reading the data,write it to the file then display the contents of the file.


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

void main() {
FILE *ptr;
char choice;
clrscr();
ptr=fopen("record.txt","w");
do
   {
  printf("\n Input Name:-");
  gets(s.name);
printf("\n input address");
gets(s.address);
printf("\n Input roll number and phone number:-");
scanf("%d%1d",&s.roll,s.phone);
fwrite(&s,sizeof(s),1,ptr);
fflush(stdin);
printf("\n Any more record:- ");
choice==getche();
}
while(choice=='y'||choice=='Y');
fclose(ptr);
ptr=fopen("record.txt","r");
printf("\n Name \t Address \t Roll \t Phone");
while(fread(&s,sizeof(s),1,ptr)==1)
  printf("\n%s \t %s \t %d \t %1d",s.name,s.address,s.roll,s.phone);
fclose(ptr);
getch();
}

2 comments: