如何使用C ++成功写入向量中的.DAT文件内容?

时间:2017-11-09 04:48:25

标签: c++ file vector data-structures visual-studio-2015

我正在尝试将.DAT文件写入我存储在名为PatientsInSystem的载体中的已保存患者列表中。但是,由于某种原因它不起作用。有谁知道我可能做错了什么?

if (!PatientsInSystem.empty()) {
            cout << "Error. There are still patients checked in. They must be checked out before quitting." << endl;
            cout << "Printing remaining patients in the system... " << endl;
            for (int i = 0; i < PatientsInSystem.size(); i++) {
                cout << "Patient's ID: " << PatientsInSystem.at(i)->getID() << endl;
                cout << "Patient's Name: " << PatientsInSystem.at(i)->getFirstName() << " " << PatientsInSystem.at(i)->getLastName() << endl;
                cout << "Patient's Birthday: " << PatientsInSystem.at(i)->getBirthDate() << endl;
                cout << "Patient's Primary Doctor's ID: " << PatientsInSystem.at(i)->getPrimaryDoctorID() << endl;
            }
        }
        else {
            outFile.open("CurrentPatients.dat", ios::out | ios::binary);
            if (!outFile.is_open()) {
                cout << "File not open." << endl;
            }
            else {
                cout << "Binary file open, saving patients now...\n";
                cout << "----------------------------------------\n";
                for (int i = 0; i < PatientsInSystem.size(); i++) {
                    Patient * p;
                    p = PatientsInSystem.at(i);
                    outFile.write(reinterpret_cast<char *> (&p), sizeof(p));
                }
            }

            outFile.close();

[下一段代码与上述相同,仅在收到第一笔帮助后编辑]

以下是代码的编辑版本...... PatientList是我的临时向量,必须清空PatientsInSystem向量才能写入文件

case 'q': {

        if (!PatientList.empty()) {
            cout << "Error. There are still patients checked in. They must be checked out before quitting." << endl;
            cout << "Printing remaining patients in the system... " << endl;
            for (int i = 0; i < PatientList.size(); i++) {
                cout << "Patient's ID: " << PatientList.at(i)->getID() << endl;
                cout << "Patient's Name: " << PatientList.at(i)->getFirstName() << " " << PatientList.at(i)->getLastName() << endl;
                cout << "Patient's Birthday: " << PatientList.at(i)->getBirthDate() << endl;
                cout << "Patient's Primary Doctor's ID: " << PatientList.at(i)->getPrimaryDoctorID() << endl;
            }
        }
        else {
            outFile.open("CurrentPatients.dat", ios::out | ios::binary);
            if (!outFile.is_open()) {
                cout << "File not open." << endl;
            }
            else {
                cout << "Binary file open, saving patients now...\n";
                cout << "----------------------------------------\n";
                for (int i = 0; i < PatientsInSystem.size(); i++) {
                    outFile.write(reinterpret_cast<char *> (PatientsInSystem.at(i)), sizeof(Patient));
                }
            }

            outFile.close();

1 个答案:

答案 0 :(得分:0)

你是否有理由使用指针数组与平面结构数组?

您正在编写本地指针变量p,而不是患者数据。您只需传递p而不是&p(或直接传递.at()),而bytes参数应为sizeof(Patient)

如果这些是指向不同大小的派生类的指针,则会出现问题。

此外,循环不会被输入,因为它在.empty()为真时输入的块中。