Tuesday, July 10, 2018

Program 9


BINARY FILE HANDLING - III


Define  a class Mobile with the following members:
          Private members:
                   Mob_id                            -        char array
                   Mob_name                      -        char array
                    Price                                -        float
          Public members:
                   void getMob ( )                -        to accept Mobile details
                   void putMob ( )               -        to display Mobile details
                   void addBook ( )              -        to add Book records to a file
                   int CheckMob(char [ ] )  -        to check if mod_id is matching
                   void disBook( )                -        to display contents of the file
    Write a user defined functions to
i)             Append Mobile records to a file
ii)           Update a Mobile record after accepting the mob_id from user
iii)          Display the Mobile records

#include<conio.h>
#include<string.h>
#include<stdio.h>
#include<fstream.h>
#include<process.h>

class Mobile
 {
          char mob_id [ 20 ] , mob_name [ 30 ];
          float price ;
    public  :
          void getMob ( )
            {
                   cout << "\nEnter Mobile ID , Mobile Name and Price:" ;
                   gets ( mob_id ) ;
                   gets ( mob_name ) ;
                   cin >> price ;
            }
          void putMob ( )
            {
                   cout << "\nMobile ID , Mobile Name and Price:" ;
                   puts ( mob_id ) ;
                   puts ( mob_name ) ;
                   cout << price ;
            }
          int checkMob ( char mid [ ] )     // fun returns 0 or 1
            {
                   return   strcmpi  (  mob_id ,  mid ) ;
            }

 } M ;


void append ( )
 {
  ofstream   fout ( "mobile . dat" , ios  ::  app | ios  ::  binary ) ;
  if ( ! fout )
   {
          cout << "\nError Opening File" ;
          getch ( ) ;
          exit ( 0 ) ;
   }
  M . getMob ( ) ;
  fout . write ( ( char * ) & M  , sizeof ( M ) ) ;
  fout . close ( ) ;
  cout << "\nMobile Details added" ;
 }
void update ( )
 {
  ifstream fin ( "mobile . dat" ,  ios  ::  binary ) ;
  if ( ! fin )
   {
    cout << "\nError Opening File" ;
    getch ( ) ;
    exit ( 0 ) ;
   }
  int f = 0 , pos = -1 ;
  char mid [ 20 ] ;
  cout << "\nEnter the ID of the mobile to be updated" ;
  gets ( mid ) ;
  while ( fin . read ( ( char *) & M , sizeof ( M ) ) )
   {
    if  ( M . checkMob ( mid ) == 0 )
     {
               f =1 ;
               pos =  fin . tellg ( ) ;
               pos = pos – sizeof ( Mobile ) ;
               break ;
     }
   }
   fin.close ( ) ;
   if ( f == 0 )
     cout << "\nMobile Id Not Found!!"  ;
   else
     { 
             M . getMob( ) ;
             ofstream fout ( "Mobile . dat" ,  ios :: binary | ios :: ate  ) ;
             fout . seekp ( pos ) ;
             fout . write ( ( char * ) & M , sizeof ( M) ) ;
             fout . close ( ) ;
             cout << “ Updated !!”;
       }
  }

void disp ( )
 {
           ifstream  fin ( "mobile . dat ",  ios  ::  binary) ;
          while( fin . read ( ( char * ) & M , sizeof ( M ) ) )
                   M . putMob ( ) ;
          fin . close ( ) ;
          getch ( ) ;
   }
    
 }

void main ( )
 {
  int ch ;
  while ( 1 )
  {
            clrscr ( ) ;
            cout << "\n         MENU\n     @*@*" ;
            cout << "\n\n\n1.       Append Mobile Records" ;
            cout << "\n2.      Update Mobile Records" ;
            cout<< ”\n3.       Display Mobile Records” ;
            cout << "\n4.      Exit " ;
            cout << "\n\n Enter choice" ;
            cin >> ch ;
            if ( ch == 1 )                  append ( ) ;
            else if ( ch == 2 )       update( ) ;
            else if ( ch == 3 )       disp ( ) ;
            else if ( ch == 4 )
             {
                   cout << "Exiting....." ;
                   getch ( ) ;
                   exit ( 0 ) ;
             }
            else 
cout << "\nInvalid Choice" ;
            getch ( ) ;
  }
 }

No comments:

Post a Comment