user3057816
user3057816

Reputation: 11

Reading value from a file, altering value and updating file

I am working on a simple wages application. I have a menu with 4 options and a text file called "shop-account" that simply contains the value 100. For option one the user is suppose to be able to transfer an amount from this 100. The user should be able to make multiple transactions but cannot overdraw the account.

Currently I have have just been opening the file and declaring the value 100 to the int "balance", then asking the user to input the amount to be transferred ("NewAmount") and simply subtracting it. However this only works for one transaction.

When I go back and try and make a second transfer it again subtracts from 100, not the updated amount. So I was wondering if anyone know how I would go about getting the file to update after each transaction?

int balance;
int NewAmount;


fstream infile;
infile.open("shop-account.txt");
infile >> balance;

do {
    cout << "1. Transfer an amount" <<endl;
    cout << "2. List recent transactions"<<endl;
    cout << "3. Display account details and current balance"<<endl;
    cout << "4. Quit" << endl;

    cout << "Please enter menu number"<<endl;

    cin >> selection;

    switch(selection) {
    case 1: 
        cout << "You have choosen to transfer an amount" << endl;
        cout << "How much do you wish to transfer from the shop account?"<<endl;
        cin >> NewAmount;
        cout << balance - NewAmount << endl;

        break;

    case 2:
        cout << "Here are you're recent transactions" <<endl;
        cout << "" << endl;
        cout << "" << endl;
        break;

    case 3:
        cout << "The account names is:" << name << endl;
        cout << "The account number is:" << number << endl;
        cout << "The current balance is\n\n" << endl; //Need to get cuurent balance still
        break;

    case 4:
        return 0;
        break;

    default:
        cout << "Ooops, invalid selection!" << endl;
        break;
    }

} while(selection != 4);

    system("pause");
return 0;
}

Upvotes: 1

Views: 175

Answers (3)

kuroi neko
kuroi neko

Reputation: 8641

Basically your file contains only one data, so doing partial updates makes no sense at all.

All you have to do is read it at the begining, like you did, and write it back entirely each time you did a transaction.

int read_balance (void)
{
    fstream f;
    f.open("shop-account.txt");
    f >> balance;
    f.close();
    return balance;
}

void write_balance (int balance)
{
    fstream f;
    f.open("shop-account.txt");
    f << balance;
    f.close();
}

and then in your code:

cout << "You have choosen to transfer an amount" << endl;
cout << "How much do you wish to transfer from the shop account?"<<endl;
cin >> NewAmount;
balance -= NewAmount;
write_balance (balance);
cout << balance << endl;

Upvotes: 2

David G
David G

Reputation: 5784

The most efficient way to do this would be to memory map the file (mmap() in Unix), update it in memory and allow the OS to flush the altered version back to disk (either periodically or on close).

Upvotes: 0

Code-Apprentice
Code-Apprentice

Reputation: 83517

To "update" a file, you have to write the whole file with the parts changed that you are "updating".

Upvotes: 0

Related Questions