Monday, June 7, 2010

file handling

#include"stdio.h"
#include"conio.h"
#include"fstream.h"
#include"stdlib.h"
#include"string.h"
#include"iomanip.h"

class book    //class defination for book
{
public:
    char name[20];     //all data member and member fun are public
    char author[20];
    int a;
    void getdata();
    void show();
};
//takes the details of book
void book::getdata()
{
 cout<<"\nENTER DETAILS ABOUT BOOK U WANT TO PURCHASE :\"";
 cout<<"\n\n\n\t\tEnter Name Of Book  :\t";
 cin>>name;
 cout<<"\n\t\tEnter Name Of Author    :\t";
 cin>>author;
 fflush(stdin);
 cout<<"\n\t\tEnter No. Of Copies  :\t";
 cin>>a;
}
void book::show()  //shows the details of book
{
    cout<<"\nBOOK      :"<
    cout<<"\nAUTHOR  :"<
    cout<<"\nCOPIES  :"<
}
void main()
{
    clrscr();
    int ch;
    book b1;//create a object of class book
do
{
    fstream f; //create object of fstream class
    f.open("book",ios::in|ios::out|ios::app);//open file in i/p,o/p and append mode
    cout<<"\nWHAT DO U WANT TO DO:"; //case
    cout<<"\n1 . TO ADD BOOK IN SHOP";
    cout<<"\n2 . TO SHOW ALL BOOKS";
    cout<<"\n3 . TO CHECK AVAILABILITY";
    cout<<"\n4 . TO MODIFY";
    cout<<"\n5 . TO EXIT";
    int a;
    cout<< "\nENTER UR CHOICE:";
     f.seekg(0);//move the file pointer to 0
    cin>>a;
    char x;
    switch (a)
    {
    case 1:
         clrscr();
         fstream f;
         f.open("book",ios::app|ios::out|ios::binary);
         char ans;
         b1.getdata();//call to getdata fun.
         f.write((char *)&b1,sizeof(b1));//write a record into file which is accept by b1 object
         getch();
         break;
 case 2:
        cout<<"\n\n";
        fstream f1;
        f1.open("book",ios::in);
        f1.seekg(0);
        while(f1.read((char *)&b1,sizeof(b1)) )
        {
        b1.show();
        if(f1.eof()==1)
        {
         break;
        }
        }
        f.close();
        break;
    case 3:
        {
        ifstream f;
         book b1;
        char name[20];
        char author[20];
        f.open("book",ios::in|ios::binary);
        cout<<"\n\n\n          Enter book name whose record to be seen  :";
        cin>>name;
     do
    {
    f.read((char *)&b1,sizeof(b1));
    if(f.eof()==1)
    {
     break;
    }
    if(strcmp(b1.name,name)==0)
    {
    cout<<"\n                  Name     :"<
    cout<<"\n                 author   :"<
    cout<<"\n                 copies   :"<
    getchar();
    }
    }while(f);
     f.close();
     }
    break;

case 4:
    {
    fstream f;
    book b1;
    char name[20];
    char author[20];
    int a;
    f.open("book",ios::in|ios::binary);
     cout<<"\n";
    cout<<"\n Enter book name whose record to be changed  :";
     cin>>name;
    do
   {
    f.read((char *)&b1,sizeof(b1));
    if(f.eof()==1)
    {
    break;
    }
     if(strcmp(b1.name,name)==0)
    {
    cout<<"\n        Name     :"<
    cout<<"\n        Author   :"<
    cout<<"\n        Copies   :" <
    getchar();
    cout<<"\n                 Enter New Values" ;
    cout<<"\n\n      Enter the book name  :";
    cin>>name;
    cout<<"\n        Enter author name    :";
    cin>>author;
    cout<<"\n        Enter no. of copies  :";
    cin>>a;
    strcpy(b1.name,name);
    strcpy(b1.author,author);
    b1.a=a;
    int l=f.tellg();
    f.close();
    f.open("book",ios::out|ios::binary|ios::ate);
    f.seekg(l-sizeof(b1));
    f.write((char *)&b1,sizeof(b1));
    }
    }while(f);
        f.close();
    }
        break;

 case 5:exit(1);
   }
}while(ch!=5);
getch();
}

No comments: