我尝试使用 while 循环读取文本文件,但它没有循环我想要的内容

问题描述 投票:0回答:0

我想让它读取预订的文件,然后将所有预订的座位标记为已预订。但它只标记第一次预订而忽略其余部分。我想知道为什么它没有循环。

[电影文件我只想读取里面有多少电影并为 for 循环获取 numMovie ... details我已经存入struct了,struct没问题...]

是不是我读取文件的方式(

getline()
)有什么问题导致我无法正常读取文件?如果是,我该如何解决?如果你能帮我解决我的问题,我将不胜感激。

这是我希望它循环读取预订文件的下一行但不起作用的代码...

string bookedTitle;
string bookedShowtime;
ifstream bookedFile("booked.txt");

if (bookedFile.is_open()) {
    while (!bookedFile.eof()) {
        getline(bookedFile, bookedTitle);
        getline(bookedFile, bookedShowtime);
        bookedFile >> row >> col;

        ifstream moviesFile("movies.txt");
        int numMovies = 0;
        string title, desc, showtime1, showtime2;
        if (moviesFile.is_open()) {
            while (getline(moviesFile, title) &&
                getline(moviesFile, desc) &&
                getline(moviesFile, showtime1) &&
                getline(moviesFile, showtime2)) {
                numMovies++;
            }
        }
        moviesFile.close();

        for (int i = 0; i < numMovies; i++) {
            for (int j = 0; j < 2; j++) {
                // Find the corresponding movie and showtime
                if (movies[i].title == bookedTitle && movies[i].showtime[j].time == bookedShowtime) {
                    int rowIdx = row - 'A';
                    int colIdx = col - 1;
                    // Mark the corresponding seat as booked
                    if (rowIdx >= 0 && rowIdx < ROWS && colIdx >= 0 && colIdx < COLS) {
                        movies[i].showtime[j].seats[rowIdx][colIdx].isBooked = true;
                    }
                }
            }

        }


    }
    bookedFile.ignore(1);//should I add this?

}
bookedFile.close();

这是我的文本文件:

Jingle Bell
1000
B3
Iron Man 
1100
F5
Jingle Bell
1000
C3

当我运行程序时,只有电影铃儿响叮当放映时间 1000 的 B3 座位会被标记为已预订...其余的没有任何变化(默认情况下未预订)

我想要的是他们三个都标记为已预订

c++ readfile
© www.soinside.com 2019 - 2024. All rights reserved.