【发布时间】:2017-01-19 09:48:42
【问题描述】:
基本上我的问题是我试图从一个充满数字和 cmets 的 .txt 文件中读取数据,并将每一行存储到一个字符串向量中,但是我的 getline 函数在第一个空白字符处停止读取,所以注释like (* comment *) 被分解成
str[0] = "(*";
str[1] = "comment";
str[2] = "*)";
这就是我的 getline 函数代码块的样子:
int main() {
string line;
string fileName;
cout << "Enter the name of the file to be read: ";
cin >> fileName;
ifstream inFile{fileName};
istream_iterator<string> infile_begin {inFile};
istream_iterator<string> eof{};
vector<string> data {infile_begin, eof};
while (getline(inFile, line))
{
data.push_back(line);
}
这就是 .txt 文件的样子:
101481
10974
1013
(* comment *) 0
28292
35040
35372
0000
7155
7284
96110
26175
我不明白为什么它没有读完整行。
【问题讨论】: