Friday, August 5, 2011

kie here's anoder prog..11) To store Book Number, Name, Price and Author. Display the file and delete a record by reading the Book Number.


#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
class book
{  private: float price;
   char bname[20];
   char author[20];
   int bno;

public:
void getdata()
{cout<<"\nEnter book no      : ";   cin>>bno;
 cout<<"\nEnter book name    : ";    gets(bname);
 cout<<"\nEnter Author name  : ";    gets(author);
 cout<<"\nEnter Price        : ";    cin>>price;
}

void putdata()
{cout<<"\nBook no      : "<<bno;
 cout<<"\nBook    Name : "<<bname;
 cout<<"\nAuthor       : "<<author;
 cout<<"\nPrice        : "<<price;
}
int retbno(){return(bno);}
}bk;

void create()
{ int n; cout<<"\nEnter no: of records"; cin>>n;
  ofstream fout;
  fout.open("Book.dat",ios::binary|ios::out);
   for(int i=0;i<n;i++)
   {bk.getdata();
    fout.write((char*)&bk,sizeof(bk));
    }
}


void delete_()
{ cout<<"\nEnter book no: of record to be deleted : ";
  int no; cin>>no;
  ifstream fin;
  fin.open("Book.dat",ios::binary|ios::in);
  ofstream fout;
  fout.open("temp.dat",ios::binary|ios::out);
  while(fin)
  {fin.read((char*)&bk,sizeof(bk));
   if(bk.retbno()!=no)
    {fout.write((char*)&bk,sizeof(bk));}
  }
    fin.close();
    fout.close();
    remove("Book.dat");
    rename("Temp.dat","Book.dat");
}

void read_and_disp()
{
 ifstream fin;
 fin.open("Book.dat");

  while(fin.read((char*)&bk,sizeof(bk)))
   {bk.putdata();break;}

}

void main()
{clrscr();
 create();
 delete_();
 read_and_disp();
 getch();
 }

Wednesday, August 3, 2011

ohk..now this is the mah first programs using files.. Its a program to To create a Binary File to store Roll Number, Name and Marks in 3 subjects of 10 students, read data from the file and display it.


#include<fstream.h>
#include<conio.h>
#include<stdio.h>
class student
{ private: int roll;
  char name[20];
  float m1,m2,m3;
  public:
void getdata()
{cout<<"\nEnter roll no : ";
 cin>>roll;
 cout<<"\nEnter name : ";
 gets(name);
 cout<<"\nEnter mark for English  : ";
 cin>>m1;
 cout<<"\n               Maths    : ";
 cin>>m2;
 cout<<"\n               Computer : ";
 cin>>m3;
}

void putdata()
{cout<<"\nStudent Roll : "<<roll;
 cout<<"\n        Name : "<<name;
 cout<<"\n        Mark of English   : "<<m1;
 cout<<"\n                Maths     : "<<m2;
 cout<<"\n                Computer  : "<<m3;
 cout<<"\n        Total marks       : "<<m1+m2+m3;
 cout<<"\n        Average marks     : "<<(m1+m2+m3)/3;

}



}stud;

void create()
{
  ofstream fout;
  fout.open("Student.dat");
  int n; cout<<"\nEnter no:of students  : "; cin>>n;

  for(int i=0;i<n;i++)
   {stud.getdata();
    fout.write((char*)&stud,sizeof(stud));
    }
}

void read_and_disp()
{
 ifstream fin;
 fin.open("Student.dat");
 while(fin)
  {fin.read((char*)&stud,sizeof(stud));
   stud.putdata();}

}

void main()
{clrscr();
 create();
 read_and_disp();
 getch();
 }