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

时间:2017-01-27 00:51:25

标签: c++ ifstream getline

我正在使用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();

    }

1 个答案:

答案 0 :(得分:1)

patientFile.open(DoctorId)需要.txt扩展名。所以patientFile.open(DoctorId +&#34; .txt&#34;)