使用 boost 文件系统 C++ 逐行读取文件

Reading a file line by line with boost filesystem C++

我正在使用 boost::filesystem 库打开具有特定路径的文件 file_path:

 fs::ifstream file(file_path);
    string str;
    vector<string> filenames;
    while(getline(file, str)){
        filenames.push_back(str);
    }

此代码改编自常规 C​​++ 代码,没有提升。我最初是在读取当前目录中的文件,但我不得不编辑路径。现在看来 getline 没有正常工作。是否有 getline 的替代方法,以便我可以逐行解析文件并将它们读入向量?

谢谢!

boost::filesystem::ifstream 只是另一个输入流,所有标准算法都以与 std::ifstream 相同的方式应用于它。 std::getline 也适用于它。

以上回答完全正确。如果有人想要完整代码:

boost::filesystem::ifstream fileHandler(fileName);
string line;
while (getline(fileHandler, line)) {
    cout << line << endl;
}