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++,所以我從來沒有抓到,哈哈。 –