ofstream正在替换整个文件,而不是在某些范围内

时间:2018-03-13 04:08:17

标签: c++ ofstream

我的程序在文件中创建客户端的100条“记录”。 在程序内,允许用户在1-100之间的任何索引处更改客户端的值。 每当我尝试更改给定索引处的值时,程序不仅会编辑边界内的记录,而且不会在它之前保留任何内容。 假设我在索引50处有一条记录,如果我尝试编辑索引1处的记录,则索引50处的记录将不存在。 我不知道如何解决这个问题,但这是代码。

#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;
    cout << client1.accountNumber - 1 << endl;
    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<const char*>(&blankClient),
                sizeof(clientData));
    }
    outCredit.close();
}

0 个答案:

没有答案