Make Your Search Easy ! :) Use me

Friday, February 17, 2017

Writing in a data file :-

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdio.h>

class student
 {
  int roll;
  char name[15];
      public:
  void get()
      {
     cout<<"\n enter roll";
     cin>>roll;
     cout<<"\n enter name";
     gets(name);
      }
  void show()
      {
     cout<<"\nRoll :"<<roll<<endl;
     cout<<"\n Name :"<<name<<endl;
       }
  };
void main()
 {
  ofstream fout("stu.dat");
  student s;
  int i;
  for(i=0;i<5;i++)
     {
    s.get();
    fout.write((char*)&s,sizeof s);
     }
 fout.close();
 ifstream fin("stu.dat");
 clrscr();
 cout<<"Details Entered into File are :\n";
 cout<<"------------------------------------------------\n\n";
        while(fin)
     {
          fin.read((char*)&s,sizeof s);
          s.show();
 }
  getch();
 }

No comments:

Post a Comment