Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 8th, 2012  |  syntax: None  |  size: 1.83 KB  |  hits: 4  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Use C   strings in file handling
  2. class budget
  3. {      
  4.      float balance;      
  5.      string due_name,loan_name;              //string objects
  6.      int  year,month;    
  7.      float due_pay,loan_given;    
  8.  
  9.      public:                
  10.      budget()    
  11.      {      
  12.           balance=0;
  13.           month=1;        
  14.           due_name="NO BODY";              //default values
  15.           loan_name="SAFE";  
  16.           year=0;            
  17.           balance = 0;      
  18.           due_pay=0;        
  19.           loan_given=0;      
  20.      }
  21.       .
  22.       .
  23.       .
  24.  };
  25.  
  26. void read_balance()                //PROBLEM AFTER ENTERING THIS FUNCTION      
  27. {          
  28.      system("cls");        
  29.      budget b;    
  30.      ifstream f1;    
  31.      f1.open("balance.dat",ios::in|ios::binary);    
  32.      while(f1.read((char*)&b,sizeof(b)))    
  33.      { b.show_data();      
  34.      }    
  35.      system("cls");        
  36.      cout<<"No More Records To Display!!";    
  37.      getch();    
  38.      f1.close();      
  39. }
  40.        
  41. basic_istream<charT,traits>& read(char_type* s, streamsize n);
  42.        
  43. // Read with no error checking :-S
  44. istream& budget::read( istream& s )
  45. {
  46.     s.read( (char*)&balance, sizeof(balance) );
  47.     s.read( (char*)&year, sizeof(year) );
  48.     s.read( (char*)&month, sizeof(month) );
  49.     s.read( (char*)&due_pay, sizeof(due_pay) );
  50.     s.read( (char*)&loan_given, sizeof(loan_given) );
  51.  
  52.     size_t length;
  53.     char *tempstr;
  54.  
  55.     // Read due_name
  56.     s.read( (char*)&length, sizeof(length) );
  57.     tempstr = new char[length];
  58.     s.read( tempstr, length );
  59.     due_name.assign(tempstr, length);
  60.     delete [] tempstr;
  61.  
  62.     // Read loan_name
  63.     s.read( (char*)&length, sizeof(length) );
  64.     tempstr = new char[length];
  65.     s.read( tempstr, length );
  66.     loan_name.assign(tempstr, length);
  67.     delete [] tempstr;
  68.  
  69.     return s;
  70. }
  71.  
  72. ostream& budget::write( ostream& s )
  73. {
  74.     // etc...
  75. }