C++ while(getline()) 无法正常工作

C++ while(getline()) not working properly

本文关键字:常工作 工作 while getline C++      更新时间:2023-10-16

我正在使用getline开始从文件中读取信息。问题是它永远不会进入 while 循环,即使文件中有数据要读取。有人看到问题可能是什么吗?我知道我的代码可能不是最好的格式,但现在我严格关注while(getline())

void loadPatients(Doctor Doctor[], int size)
{
    std::ifstream patientFile;
    Patient** patientInfo;
    std::string Address, DoctorId, Id, Name, PhoneNumber,junk;
    int patientNumber;
    patientInfo = new Patient*[size];
    for (int b = 0; b < size; b++)
        patientInfo[b] = new Patient[10];
    for (int i = 0; i < size; i++)
    {
        DoctorId = Doctor[i].getId();
        patientFile.open(DoctorId);
        patientNumber = Doctor[i].getNumberOfPatient();
        for (int a = 0; a < patientNumber; a++)
        {
            while (getline(patientFile, Name))
            {
                getline(patientFile, Id);
                getline(patientFile, Address);
                getline(patientFile, PhoneNumber);
                getline(patientFile, DoctorId);
            }
            patientInfo[i][a].setAddress(Address);
            std::cout << Address;
            patientInfo[i][a].setName(Name);
            patientInfo[i][a].setDoctorId(DoctorId);
            patientInfo[i][a].setId(Id);
            patientInfo[i][a].setPhoneNumber(PhoneNumber);
        }
        patientFile.close();
    }

patientFile.open(DoctorId) 需要它的.txt扩展名。 所以patientFile.open(DoctorId+".txt")