【发布时间】:2019-11-08 22:52:21
【问题描述】:
两个 while 循环单独工作。但是,当上面的 while 循环存在时,我的代码中的第二个 while 循环将不起作用。我需要两个 while 循环一起工作。
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main(){
string line, line1, line2;
int count = 0;
int track = 0;
int stop;
ifstream file ("ai.txt");
while (getline(file, line2)){
count++;
}
file.seekg (0L, ios :: beg);
stop = count - 10;
while (getline(file, line)){
track++;
if (track >= stop){
for (int num = 1; num <=10; num++){
getline(file,line1);
cout << line1 << endl;
}
}
}
}
代码应该输出任何文本文件的最后十行。
【问题讨论】:
标签: c++ file loops while-loop text-files