fstream に Boost.Filesystem の path を渡す

Boost.Filesystem に path を受け取る fstream が定義されているらしいです。

[ソース]

#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>

int
main(int argc, char const* argv[]){
    namespace fs = boost::filesystem;
    
    fs::path exe = argv[0];

    fs::ifstream ifs(exe.parent_path()/"main.cpp");
    if( ifs.fail() ){
        std::cout << "Not found" << std::endl;
        return 1;
    }

    std::string line;
    while(std::getline(ifs, line)){
        std::cout << line << std::endl;
    }

    return 0;
}

[出力]

#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>

int
main(int argc, char const* argv[]){
    namespace fs = boost::filesystem;
    
    fs::path exe = argv[0];

    fs::ifstream ifs(exe.parent_path()/"main.cpp");
    if( ifs.fail() ){
        std::cout << "Not found" << std::endl;
        return 1;
    }

    std::string line;
    while(std::getline(ifs, line)){
        std::cout << line << std::endl;
    }

    return 0;
}

[boost]

  • ver 1.49.0