Cameron Hamilton
Cameron Hamilton

Reputation: 29

How can I use ofstream to write to a file multiple times without wiping the file clean?

I am trying to make a bank account management system and I am having an issue when trying write more than one account's info to a file.

//Globals
class Account
{
public:
    Account() { "\n\tConstructing Account"; }
    ~Account() { "\n\tDeconstructing Account"; }

    //Accessor Methods
    void setPersonName(string X) { personName = X; }
    void setAccType(string X) { accType = X; }
    void setaccBalance(string X) { accBalance = X; }
    string getPersonName() { return personName; }
    string getAccType() { return accType; }
    string getAccBalance() { return accBalance; }


private:
    string personName;
    string accType;
    int accNumber[25];
    string accBalance;
};

//Function Prototypes
void createAcc();
void editAcc();
void saveAcc();
void closeAcc();
void dispAcc();
void loadAcc();

Account * CH;

int main()
{
    char choice[10];
    CH = new Account();

    cout << "\n\t Banking Management System 1.0\n";

    while (choice[0] != 'q')
    {
        cout << "\n\t---------------Main Menu---------------------";
        cout << "\n\t|                                            |";
        cout << "\n\t|          (O)PEN NEW ACCOUNT                |";
        cout << "\n\t|          (E)DIT ACCOUNT INFO               |";
        cout << "\n\t|          (D)ISPLAY ACCOUNT INFO            |";
        cout << "\n\t|          (L)OAD FILE                       |";
        cout << "\n\t|          (S)AVE ACCOUNT TO FILE            |";
        cout << "\n\t|          (C)LOSE ACCOUNT                   |";
        cout << "\n\t|          (Q)UIT                            |";
        cout << "\n\t|                                            |";
        cout << "\n\t----------------------------------------------\n\n\t";

        cin >> choice;

        switch (choice[0])
        {
        case 'o': createAcc();  break;
        case 'e': editAcc();  break;
        case 'd': dispAcc();  break;
        case 'l': loadAcc();  break;
        case 's': saveAcc();  break;
        case 'c': closeAcc();  break;
        case 'q': break;
        }
    }


    system("PAUSE");
    return 0;
}
//-----------------------------------------------------------------------------------------
void createAcc()
{
    CH = new Account();
}
//-----------------------------------------------------------------------------------------
void editAcc()
{
    string temp;
    system("CLS");
    cout << "\n\t---------------EDIT ACCOUNT---------------";

    cout << "\n\tName: ";
    cin.ignore();
    getline(cin, temp);
    CH->setPersonName(temp);

    cout << "\n\tAccount Type: ";
    getline(cin, temp);
    CH->setAccType(temp);

    cout << "\n\tAccount Balance: ";
    getline(cin, temp);
    CH->setaccBalance(temp);
}
//-----------------------------------------------------------------------------------------
void dispAcc()
{
    system("CLS");
    cout << "\n\n\t----------ACCOUNT INFORMATION-----------";
    cout << "\n\tName: " << CH->getPersonName();
    cout << "\n\tAccount Type: " << CH->getAccType();
    cout << "\n\tBalance: " << CH->getAccBalance();
    cout << "\n\t-----------------------------------------\n\n";
}
//-----------------------------------------------------------------------------------------
void loadAcc() {
    try
    {
        string temp;
        ifstream accFile;
        accFile.open("credentials.file", ios::in);

        getline(accFile, temp);
        CH->setPersonName(temp);

        getline(accFile, temp);
        CH->setAccType(temp);

        getline(accFile, temp);
        CH->setaccBalance(temp);

        accFile.close();
    }
    catch (exception X) {
        cout << "\n\tFile ERROR! Unable to load data.";
    }
}

//-----------------------------------------------------------------------------------------
void saveAcc()
{
try
    {
        ofstream accFile;
        accFile.open("credentials.file", ios::out);

        accFile << CH->getPersonName() << "\n";
        accFile << CH->getAccType() << "\n";
        accFile << CH->getAccBalance() << "\n";

        accFile.close();
        cout << "\n\tSuccess! Data was saved to file";
    }
    catch (exception X)
    {
        cout << "\n\tError! Could not save account details";
    }
}

When I create a new account and edit it's info once I save the file it overwrites the other account in the file. I want to be able to create multiple accounts and write all the data to a file so that I can then pic and choose which account to view. How can I achieve this? If need be could I get the program to write each new account to a separately named file and load them this way? Really stuck on this would appreciate any help I can get!

Upvotes: 0

Views: 918

Answers (2)

ricco19
ricco19

Reputation: 743

Take a look at ios::app, you probably want to do something like this:

accFile.open("credentials.file", ios::out | ios::app);

This will make sure to seek to the end of the file before every write. Using only ios::out, it will just rewrite everything wherever you seek too (which is the beginning of the file if you are not explicit about it).

Upvotes: 2

Max Vollmer
Max Vollmer

Reputation: 8598

From the documentation:

app (append) Set the stream's position indicator to the end of the stream before each output operation.

So do this:

accFile.open("credentials.file", ios::out | ios::app);

Upvotes: 1

Related Questions