c++ file output...

Associate
Joined
12 Jun 2003
Posts
898
Location
Wicklow, Ireland
c++ file input/output...

i'm having a little trouble with file output in c++ just wondering can you guys help me along. the program itself is fairly trivial.

i set up two bank_account objects in main()

acc1 and acc2. i initialise them with values for account#, sortcode, pin, balance, overdraft_limit.


Code:
main()
{
      bank_account acc1(12345, 901047, 6472, 100.25, 10000) ; 
      bank_account acc2(123, 901, 1234, 150.25, 100) ;
           
      acc1.write_data() ;
      acc2.write_data() ;  
}

the write_data function is part of the bank_account class.

here it is...

Code:
void bank_account::write_data() 
{
     ofstream out ;
     out.open( "test.txt" ) ;
     out << *this ;
     out << endl ; 
     out.close() ;   
}

now the write_data function works fine with no problems but it only writes the last object into the file "text.txt".

so test.txt looks like this

Code:
123 901 1234 150.25 100

whereas i'd like it to be this...

Code:
12345 901047 6472 100.25 10000
123 901 1234 150.25 100

here is my overloaded << operator for a bank_account object...

Code:
ostream& operator<<(ostream& os, const bank_account& acc )
{
  long acc_num; 
  long sort ; 
  int pin ; 
  double bal ; 
  double overdraft ; 
  
  acc.get_details(acc_num, sort, pin, bal, overdraft) ;
  os << acc_num << " " << sort << " " << pin << " " << bal << " " << overdraft << " " << endl ;
  return os ;
}


can anyone advise me as to where i'm going wrong?
 
Last edited:
open the file with append mode instead?
Code:
out.open("test.txt", ios::out | ios::app);
 
sorry for dragging this up again but i have a few more questions...

i have a .txt file that contains info about bank accounts

here's an example of the file that contains the accounts.

Code:
pin----card-----accno-----sort----od---bal
1234 1234567 0123456789 901047 100 00400
1111 1111111 1111111111 111111 100 00450
4321 7654321 1876543210 901047 500 10450

now i have the program asking for the card number and pin number. it reads the file and if there's a match it loads the rest of that account into a bank_account object.

after this i can change the balance amount using withdraws or deposits.

what i want to be able to do is to find the account i'm using in the file, and when all the withdrawals/deposits are finished i would like to update the balance figure only.

i know that ios::app only tags information on to the end of the file, which would be grand but is there any way of overwriting information from the file??

other question is this...

i have another file which tracks all the transactions (withdrawl/deposits) and which account number they belong to.

an example of this file
Code:
03/18/07 00:43:49 123456789 (100.00) 300.00
03/18/07 00:44:05 123456789 +100.00 500.00
03/18/07 02:14:21 123456789 (100.00) 300.00
03/18/07 02:17:12 123456789 (100.00) 300.00
03/18/07 02:34:49 123456789 (45.00) 355.00
03/18/07 02:35:48 1876543210 (1000.00) 9450.00
03/18/07 02:36:44 1876543210 (11.00) 10439.00

what i want to be able to do is pick out the transactions specific to the account.

here's what i have so far....

Code:
void transaction::mini_statement(bank_account& acc)
{
     
     string temp_date ;
     string temp_time ;
     long temp_accno ; 
     string temp_trans ; 
     double temp_bal ;

     
     ifstream in ; 
     in.open("transactions.txt") ;
     
     
     if(in.fail())
     {
        cerr << "cannot find transactions.txt" << endl ;
     }

     do
     {
                
        in >> temp_date >> temp_time >> temp_accno >> temp_trans >> temp_bal  ;
      
        
        
        if(temp_accno == acc.return_accno())
           {
                 cout << temp_date << " " << temp_time << " " << temp_trans << " " << temp_bal << endl ;
           }

    } while( !(in.eof()) );
    
}

now i know all that will do is match the account number of the account in the parameter and it will print out a list of all the transactions. how could i go about reading just the last five transactions of that account number?

any replies are greatly appreciated.
 
Last edited:
Just an idea here... What about putting the data into a struct. Put all the data from the file into the vector and then make a for loop with vector.size() down to vector.size()-5 and print vector[vector.size()] to vector[vector.size()-5]. Could work...
 
I'd recommend you learn about databases and SQL :p
But if that's a restriction, you could pull in all the data from the text into an array, find your acc_num in the array, overwrite that item, then output the lot to the text file.
Its inefficient though - look for any kind of line by line handling of text files (google search would be a starting point).
Good luck.
 
Back
Top Bottom