Saturday, 2 June 2012

C++ Program to create a data file and read it as well.

C++ Program to create a data file and read it as well.

#include<fstream.h>
#include<conio.h>
main()
{
clrscr();
ofstream fat("student.dat");
char name[20];
char hobby[40];
int age;
int i;
for (i=1;i<6;i++)
{
cout<<"Enter the details of record "<<i;
cout<<endl;
cout<<"Enter name:";
cin>>name;
cout<<endl<<"Enter the hobby";
cin>>hobby;
cout<<endl;
cout<<"Enter the age";
cin>>age;
cout<<endl;
fat<<name<<hobby<<age;
cout<<endl;
}
fat.close();

// opening of the file in input mode
ifstream fit ("student.dat");
for(i=1;i<6;i++)
{
fit>>name;
fit>>hobby;
fit>>age;
cout<<"The record number is "<<i;
cout<<endl<<"The name is "<<name;
cout<<endl<<"The hobby is "<<hobby;
cout<<endl<<"The age is "<<age;
cout<<endl;
}
fit.close();
getch();
return 0;
}

0 comments:

Post a Comment

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More