Reading in the last few characters of a file problems

This is a discussion on Reading in the last few characters of a file problems within the C++ Programming forums, part of the General Programming Boards category; I've been working on a small bank-like program that writes to a file like this: Opening Balance >>>>> $546.50 Deposit ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    2

    Question Reading in the last few characters of a file problems

    I've been working on a small bank-like program that writes to a file like this:

    Opening Balance >>>>> $546.50
    Deposit >>>>>> $52.50; New Balance: $599.00
    Withdrawl >>>>>> $49.00; New Balance: $550.00

    The writing to the file part works great, but when I re-open the file, I need to somehow grab the balance from the last time, otherwise I just start printing the last spot in memory when I call the function to display the balance (obviously). I am not really great with file manipulation yet (still learning a lot). Is there anyone who can give me an idea on how to grab the balance from the file when I open it again?

    Thanks,
    Dustin

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    139
    here is a sample prog for yah
    Code:
    #include <iostream.h>
    #include <fstream.h>
    
    int main()
    {
      ifstream inFile;
      char temp;
      float start_balance;
    
      inFile.open("bank.txt");
      while(inFile)
      {
        do
        {
          inFile >> temp;
        }while (temp!='$');
        inFile >> start_balance;
      }
      inFile.close();
      cout << "Last Balance was : $" <<start_balance;
      return 0;
    }
    and the bank.txt contains this:
    Code:
    Opening Balance >>>>>> $546.50 
    Deposit >>>>>> $52.50; New Balance: $599.00
    Withdrawl >>>>>> $49.00; New Balance: $550.00
    cheers

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    206

    Talking

    Code:
    #include <iostream.h>
    #include <fstream.h>
    
    main()
    {
          int OpenBalance, Deposit, Withdrawal, NewBalance, NewBalanceTwo;
    
          ifstream fin;
          fin.open("bank.txt");
          fin<<OpenBalance;
          fin<<Deposit;
          fin<<Withdrawal;
          fin<<NewBalance;
          fin<<NewBalanceTwo;
          fin.close();
    
          cout<<"Opening Balance: $"<<OpenBalance;
          cout<<"\nDeposit: $"<<Deposit<<"       New Balance: $"<<NewBalance;
          cout<<"Withdrawal: $"<<Withdrawal<<"      New Balance: $"<<NewBalanceTwo;
    
          cin<<NewBalance;
    
          return 0;
    }
    and when you save to the file use this:

    Code:
    ofstream fout;
    fout.open("bank.txt");
    fout>>OpenBalance;
    fout>>Deposit;
    fout>>Withdrawal;
    fout>>NewBalance;
    fout>>NewBalanceTwo;
    fout.close();
    make sure your numbers are stored in the variables i used, or jsut switch my variable names to yours.

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    2

    Thumbs up THANK YOU!!! Works! <eom>

    Thanks a lot all!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 05:54 PM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 09:39 AM
  3. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  4. I have some problems reading struc from file
    By samc2004 in forum C Programming
    Replies: 4
    Last Post: 12-18-2003, 02:47 PM
  5. reading characters in a file
    By jane in forum C Programming
    Replies: 2
    Last Post: 03-25-2003, 07:10 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21