1
基本上我的问题是,我试图从一个.txt文件中读取数据,该文件充满数字和注释,并将每行存储到一个字符串向量中,但是我的getline函数停止读取在第一个空格字符所以像(*注释*)评论被分成C++:Getline首先停止读取空格
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
我不明白为什么它不读取整行。
哇。非常感谢,难怪我被抛弃了 - 这是我的老师告诉我们使用的例子,同时也告诉我们使用getline。相当新的C++,所以我从来没有抓到,哈哈。 –