每次迭代都会覆盖ofstream文件

时间:2018-03-12 19:16:32

标签: c++ ifstream

我的程序试图创建一个新的"记录"在用户指定的索引处。一旦用户创建了这条新记录,所有旧记录都会被覆盖,我不确定原因。

在代码中,我试图创建两个新记录,第一个记录不再在文件中,但第二个记录不在。

如果我只做一个记录,那么该程序运行正常,但只有当我尝试将两个或更多记录添加到credits.dat然后它才会覆盖所有内容时

#include <fstream>
#include <iostream>

using namespace std;
struct clientData {
    int accountNumber;
    char lastName[15];
    char firstName[10];
    float balance;
};

void initializeFile();
void getClientInfo();
void printAllInfo();
void printUserByIndex(int);

int main() {
    initializeFile();
    getClientInfo();
    getClientInfo();

    cout << "Which index would you like to get the data at?" << endl;
    int userIndex;
    cin >> userIndex;
    printUserByIndex(userIndex);
    printAllInfo();
    return 0;
}

void printAllInfo() {
    ifstream inCredit("credit.dat", ios::in);
    clientData lastClient;
    for (int i = 1; i < 100; i++) {
        clientData newClient;
        inCredit.seekg((i - 1) * sizeof(clientData));
        inCredit.read(reinterpret_cast<char*>(&newClient), sizeof(clientData));
        cout << "Account number: " << newClient.accountNumber << endl;
        cout << "First name: " << newClient.firstName << endl;
        if (newClient.accountNumber != 0
                && lastClient.accountNumber != newClient.accountNumber) {
            cout << "Account number: " << newClient.accountNumber << endl;
            cout << "First name: " << newClient.firstName << endl;
            cout << "Last name: " << newClient.lastName << endl;
            cout << "Account balance: " << newClient.balance << endl;
            lastClient = newClient;
        }
    }
}

void printUserByIndex(int index) {
    ifstream inCredit("credit.dat", ios::in);
    clientData newClient;
    inCredit.seekg((index - 1) * sizeof(clientData));
    inCredit.read(reinterpret_cast<char*>(&newClient), sizeof(clientData));
    cout << "Account number: " << newClient.accountNumber << endl;
    cout << "First name: " << newClient.firstName << endl;
    cout << "Last name: " << newClient.lastName << endl;
    cout << "Account balance: " << newClient.balance << endl;
    cout << endl;
}

void getClientInfo() {
    clientData client1;
    ofstream outCredit1("credit.dat", ios::ate);
    cout << "Please enter a number between 1 and 100!" << endl;
    cin >> client1.accountNumber;
    cout << "Please enter the client's first name" << endl;
    cin >> client1.firstName;
    cout << "Please enter the client's last name" << endl;
    cin >> client1.lastName;
    cout << "Please enter the client's balance name" << endl;
    cin >> client1.balance;
    outCredit1.seekp((client1.accountNumber - 1) * sizeof(clientData));
    outCredit1.write(reinterpret_cast<const char*>(&client1),
            sizeof(clientData));
    outCredit1.close();
}

void initializeFile() {
    ofstream outCredit("credit.dat", ios::out);
    clientData blankClient = { 0, "", "", 0.0 };
    for (int i = 0; i < 100; i++) {
        outCredit.write(reinterpret_cast<char *>(&blankClient.accountNumber),
                sizeof(int));
        outCredit.write(blankClient.lastName, sizeof(char) * 30);
        outCredit.write(reinterpret_cast<char *>(&blankClient.firstName),
                sizeof(char) * 10);
        outCredit.write(reinterpret_cast<char *>(&blankClient.balance),
                sizeof(float));
    }
    outCredit.close();
}

2 个答案:

答案 0 :(得分:0)

在getClientInfo

中尝试使用ios :: app而不是ios :: ate

答案 1 :(得分:0)

我们需要告诉计算机打开文件的目的。例如 - 在文件上写入,从文件中读取等。这些是我们可以打开文件的不同模式。

(ios :: app) - 打开要附加的文本文件。 (追加意味着在最后添加文字)。

(ios :: ate) - 打开一个文件进行输出,并将读/写控制移动到文件的末尾。

(ios :: in) - 打开一个文本文件进行阅读。

(ios :: out) - 打开一个文本文件进行写作。

(ios :: trunc) - 如果文件存在,则在打开文件之前截断内容。

void initializeFile() {
  ofstream outCredit("credit.dat", ios::app);
  clientData blankClient = { 0, "", "", 0.0 };
  for (int i = 0; i < 100; i++) {
     outCredit.write(reinterpret_cast<char *>(&blankClient.accountNumber),
             sizeof(int));
     outCredit.write(blankClient.lastName, sizeof(char) * 30);
     outCredit.write(reinterpret_cast<char *>(&blankClient.firstName),
             sizeof(char) * 10);
     outCredit.write(reinterpret_cast<char *>(&blankClient.balance),
            sizeof(float));
   }
  outCredit.close();
 }

ios::app将在您的第一个代码之后编写代码。 ofstream outCredit("credit.dat", ios::app)

如果你想从文件中替换特定元素,那么你将加载程序并在修改后写入文件。

如果您想询问有关文件处理的更多信息,请注意。