【发布时间】:2013-08-19 21:28:58
【问题描述】:
我正在尝试使用 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);
}
【问题讨论】:
-
另外,这篇文章的标题与您遇到的问题有什么关系..?
-
那是因为我在做了一些研究之后没有编辑它然后遇到了一个不同的问题。我想知道为什么我没有得到任何输出。