我花了将近 4 个小时试图解决这个问题......
我有一个超过 100 行的文本文件。每行有 4 个值,以逗号分隔。我希望能够提取每个值并将其保存到变量(v1...v4)中。
我使用了 for 循环,因为我不会读取文件的全部内容。我现在只是想让 1 工作。
到目前为止,我已经成功读取了一行。我现在只需要打破争吵。这是我的大学作业,不允许我使用任何 boost 或 tokeniser 类。只需 getline 和其他基本命令。
我有这个代码:
// Read contents from books.txt file
ifstream theFile("fileName.txt");
string v1, v2, v3, v4, line;
for (int i = 0; i < 1; i++) {
getline(theFile, line, '\n');
cout << line << endl; // This part works fine
getline(line, v1, ","); // Error here
cout << v1 << endl;
getline(line, v2, ","); // Error here
cout << v2 << endl;
getline(line, v3, ","); // Error here
cout << v3 << endl;
getline(line, v4, '\n'); // Error here
cout << v4 << endl;
}
theFile.close();
我得到的错误是 - 错误:没有调用“getline(std::string&, std::string&, const char [2]) 的匹配函数”
我怎样才能解决这个问题?