ファイル・パス操作

ファイルが存在するかどうかを調べたい

標準ライブラリではできないのでBoostを使用する。

1
2
3
4
5
6
7
8
9
#include <boost/filesystem.hpp>

int main() {
    if (boost::filesystem::exists("file.txt")) {
        // file.txtが存在する
    }

    return 0;
}

ファイル名から拡張子を取り出したい

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
#include <string>

int main() {
    std::string filename = "file.txt";

    // ext == ".txt"
    const std::string& ext = filename.substr(filename.rfind("."));

    return 0;
}