用于读取文本文件C++的RealLoad循环

用于读取文本文件C++的RealLoad循环,c++,C++,我试图在读取文本文件时只循环代码的某一部分,因为第一行是唯一唯一的一行。其余的行重复它们被分配到的变量类型和它们应该表示的内容。我试过多次添加break,但只修复了一半。因为我不认为使用break是正确的,所以哪种方法更有效这是我指的代码 void RegOffice::analyzeFile() { cout << "Please enter the location of the file to be read. " << endl; cin >

我试图在读取文本文件时只循环代码的某一部分,因为第一行是唯一唯一的一行。其余的行重复它们被分配到的变量类型和它们应该表示的内容。我试过多次添加break,但只修复了一半。因为我不认为使用break是正确的,所以哪种方法更有效这是我指的代码

void RegOffice::analyzeFile()
{
    cout << "Please enter the location of the file to be read. " << endl;
    cin >> fileName;
    cout << endl;
    inData.open(fileName.c_str());

    if (inData.is_open())
    //while file is open
    {
        int c;
        while(inData)       //While file is still good
        {

            /* 
            The function below reads in one int at a time on each line. First line is the number of windows
            open. The next line will be the time (or clock tick) at which the next student(s) arrive. 
            The next line will be the number of students that arrive at that time. The lines after that
            will be the amount of time each student needs at a window in minutes.
            */


            inData >> winOpen;  //Gets the number of windows open
            cout << "Windows Open: " << winOpen << endl;            
            windows = new GenQueue<Window>(winOpen);    //Sets up an array of open windows
            while(!inData.eof())  //This code below is what I want to loop through separately (rather than the above code being called again)
            {
                inData >> time;     //Gets the first time that students arrive
                cout << "Time: " << time << endl;   
                inData >> numStudents;      //Gets the number of students that enter the line at that time
                cout << "Number of Students at time " << time << " is " << numStudents << endl; 

                for(int i = 0; i < numStudents; i++)    // numStudents is the number read in by the text file
                {
                    Student *stu = new Student(); //creating a new instance of a student
                    inData >> c;
                    stu->setTimeAtWin(c);  //Setting student's wait time to the next number in file
                    stu->setArrivalTime(time);   //Setting student's arrival time
                    cout << "New Student created! Info for Student #" << i + 1 << ":" << endl;
                    stu->print();
                    line->addBack(*stu);    //Inserting that student to the back of the queue (The first student                                                //will be first in line though if no one is in line)
                    cout << "Number of people in line: " << line->getSize() << endl;
                }

            }
        }
        inData.close();
    }
    else    //Shows error message
        cout << "Error opening file! Please try again!" << endl;
}

您可以创建一个变量,以便在执行while循环时跟踪读取行的状态

然后,调整while循环逻辑,并更改状态

枚举文件读取状态{加载\数据,加载\数据\完成}

如果文件中有特殊单词,则使用If语句查找它们,并相应地切换FileReadingState的状态。

您应该阅读以下内容: