我正在尝试使用 getline 从文件中读取行,然后显示每一行。但是,没有输出。输入文件是 lorem ipsum 虚拟文本,每个句子都有新行。这是我的代码:
#include <vector>
#include <string>
#include <fstream>
#include <iostream>
using namespace std;
int main() {
string line;
vector<string> theText;
int i = 0;
ifstream inFile("input.txt");
if(!inFile)
cout << "Error: invalid/missing input file." << endl;
else {
while(getline(inFile, line)) {
theText[i] = line;
theText[i+1] = "";
i += 2;
}
//cout << theText[0] << endl;
for (auto it = theText.begin(); it != theText.end() && !it->empty(); ++it)
cout << *it << endl;
}
return (0);
}