如何在不擦除文件干净的情况下多次使用ofstream写入文件?

时间:2018-04-22 02:33:31

标签: c++ fstream file-handling ofstream

我正在尝试建立一个银行帐户管理系统,在尝试将多个帐户的信息写入文件时遇到问题。

//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";
    }
}

当我创建一个新帐户并在保存文件后编辑它的信息时,它会覆盖文件中的另一个帐户。我希望能够创建多个帐户并将所有数据写入文件,以便我可以拍照并选择要查看的帐户。我怎样才能做到这一点?如果需要,我可以让程序将每个新帐户写入单独命名的文件并以这种方式加载它们吗?真的坚持这一点会感谢我能得到的任何帮助!

2 个答案:

答案 0 :(得分:2)

看一下ios::app,你可能想做这样的事情:

{{1}}

这将确保在每次写入之前寻找文件的末尾。只使用ios :: out,它只会重写你所寻找的所有内容(如果你没有明确的话,那就是文件的开头)。

答案 1 :(得分:1)

来自documentation

  

app(append)将流的位置指示器设置为   在每次输出操作之前流。

这样做:

        let alert = this.alerts.create({
            title: 'Error',
            subTitle: 'Username/password is invalid!',
            buttons: ['Dismiss']
        });

cordova.plugin.http.sendRequest('http://127.0.0.1:5000/api/login/', options, function(response) {

        try {   //onSuccess
            response.data = JSON.parse(response.data);

            localStorage.setItem('token', JSON.stringify(response.data.token));

          } catch(e) {
            console.error('JSON parsing error');
          }
        },  function(response) {    //onFail
            console.log('403');
            alert.present();

        }
    );