简体   繁体   English

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

[英]How to successfully write to a .DAT file contents in a vector using C++?

I'm trying to write to a .DAT file my list of saved patients that are stored in a vector named PatientsInSystem. 我正在尝试将保存在名为PatientInSystem的向量中的已保存患者列表写入.DAT文件。 However, it won't work for some reason. 但是,由于某些原因,它将无法正常工作。 Does anyone know what I might be doing wrong? 有人知道我可能做错了吗?

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();

[This next section of code is the same as above only edited after the first amount of help received] [下一部分代码与上述相同,仅在获得第一笔帮助后才进行编辑]

Here is the edited version of the code... PatientList is my temporary vector which must be emptied for PatientsInSystem vector to write to the file 这是代码的编辑版本... PatientList是我的临时载体,必须清空以使PatientInSystem矢量写入文件

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();

Is there a reason you are using an array of pointers vs a flat array of structs? 您是否有理由使用指针数组与平面数组?

You are writing the local pointer variable p , instead of the Patient data. 您正在编写局部指针变量p ,而不是Patient数据。 You would just pass p instead of &p (or just pass .at() directly), and the bytes parameter should be sizeof(Patient) . 您只需传递p而不是&p (或直接传递.at()),并且bytes参数应该是sizeof(Patient)

If these are pointers to different sized derived classes, this has issues. 如果这些是指向不同大小的派生类的指针,则会出现问题。

Also, the loop won't every be entered, because its in the block entered when .empty() is true. 另外,不会每次都输入循环,因为当.empty()为true时,在所输入的块中输入了该循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM