我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么

I'm using a nested while loop to Parse a text file with multiple lines but for some reason, it only goes through the first line and I don't know why

本文关键字:由于某种原因 我不知道 为什么 一行 文件 循环 while 嵌套 文本      更新时间:2023-10-16


我使用嵌套的while循环来分析一个有多行的文本文件,但由于某种原因,它只经过第一行,我不知道为什么。

string file;
string line;
cout << "Whats the file name?" << endl;
cin >> file;

string inputStr;
string buf; // Have a buffer string
stringstream s; // create the string stream
int field = 0;
string input; //string for the input (i.e. name, ID, etc.)

ifstream InFile(file.c_str());
if (InFile.is_open()) {
cout << "File found" << endl;

while (getline(InFile, line)) {
cout << line << endl;
inputStr = line;
s << inputStr; //put the line into the stream
while (getline(s, input, ' ')) { //gets a string from the stream up the next comma 
field++;
cout << " field" << field << " is " << input << endl;
}
cout << "DONE" << endl;

}
  1. 在需要使用变量时声明它们
  2. 你说while (getline(s, input, ' ')) { //gets a string from the stream up the next comma,但你给出了第三个自变量' '(一个空格(。我想这应该是个逗号吧

固定代码:

cout << "File found" << endl;
while (getline(InFile, line)) {
cout << line << endl;
stringstream s(line); // create the string stream and init with line
while (getline(s, input, ',')) { //gets a string from the stream up the next comma 
field++;
cout << " field" << field << " is " << input << endl;
}
cout << "DONE" << endl;
}
}
相关文章: