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

jon*_*d42 3 filesystems parsing boost file

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

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

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

谢谢!

Maz*_*MIK 7

上面的答案是绝对正确的。如果有人想要完整的代码:

boost::filesystem::ifstream fileHandler(fileName);
string line;
while (getline(fileHandler, line)) {
    cout << line << endl;
}
Run Code Online (Sandbox Code Playgroud)


And*_*hev 5

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