ファイル システムのナビゲーション

<filesystem> ヘッダーは、File System Technical Specification ISO/IEC TS 18822:2015 (最終ドラフト: ISO/IEC JTC 1/SC 22/WG 21 N4100) を実装し、型および関数により、ファイル システムを移動するためのプラットフォームに依存しないコードを記述できます。 それはクロス プラットフォームなので、Windows システムに関連しない API が含まれます。 たとえば、is_fifo(const path&) は Windows では常に false を返します。

概要

次のタスクには <filesystem> API を使用します。

  • 指定のパスの下のファイルとディレクトリを反復処理します。

  • ファイルの作成時、サイズ、拡張子、およびルート ディレクトリなどに関するファイルの情報を取得します。

  • パスの構成、分解、および比較

  • ディレクトリの作成、コピー、削除

  • ファイルのコピーと削除

標準ライブラリを使用したファイル IO の詳細については、「iostream プログラミング」を参照してください。

パス

パスの構成および描画

(XP 以降の) Windows のパスは、ネイティブで Unicode で格納されます。 path クラスは、自動的にすべての必要な文字列の変換を実行します。 それはワイド文字とナロー文字の両方の配列の引数を受け入れ、UTF8 や UTF16 として書式設定された std::stringstd::wstring のどちらの型も受け入れます。 path クラスは、パスの区切り記号も自動的に正規化します。 コンストラクターの引数のディレクトリの区切り記号として単一のスラッシュを使用することができます。 この区切りにより、Windows と UNIX の両方の環境でのパスの格納に、同じ文字列を使用することができます。

path pathToDisplay(L"/FileSystemTest/SubDir3");     // OK!
path pathToDisplay2(L"\\FileSystemTest\\SubDir3");  // Still OK as always
path pathToDisplay3(LR"(\FileSystemTest\SubDir3)"); // Raw string literals are OK, too.

2 つのパスを連結する際、オーバーロードされた //= 演算子を使用できますが、これは + および +=std::stringstd::wstring演算子と似ています。 path オブジェクトは、ユーザーが区切り文字を指定していない場合に、便利にそれを指定できます。

path myRoot("C:/FileSystemTest");  // no trailing separator, no problem!
myRoot /= path("SubDirRoot");      // C:/FileSystemTest/SubDirRoot

パスの確認

path クラスには、パス自体のさまざまな部分に関する情報を返すメソッドがいくつかあります。 この情報は、参照される可能性があるファイル システム エンティティに関する情報とは異なります。 ルート、相対パス、ファイル名、および、ファイル拡張子などを取得できます。 階層内のすべてのフォルダーを確認するために、パス オブジェクトで反復処理することができます。 次の例は、パス オブジェクトを反復処理する方法を示しています。 また、その部分に関する情報を取得する方法も示します。

// filesystem_path_example.cpp
// compile by using: /EHsc /W4 /permissive- /std:c++17 (or later)
#include <string>
#include <iostream>
#include <sstream>
#include <filesystem>

using namespace std;
using namespace std::filesystem;

wstring DisplayPathInfo()
{
    // This path may or may not refer to an existing file. We are
    // examining this path string, not file system objects.
    path pathToDisplay(L"C:/FileSystemTest/SubDir3/SubDirLevel2/File2.txt ");

    wostringstream wos;
    int i = 0;
    wos << L"Displaying path info for: " << pathToDisplay << endl;
    for (path::iterator itr = pathToDisplay.begin(); itr != pathToDisplay.end(); ++itr)
    {
        wos << L"path part: " << i++ << L" = " << *itr << endl;
    }

    wos << L"root_name() = " << pathToDisplay.root_name() << endl
        << L"root_path() = " << pathToDisplay.root_path() << endl
        << L"relative_path() = " << pathToDisplay.relative_path() << endl
        << L"parent_path() = " << pathToDisplay.parent_path() << endl
        << L"filename() = " << pathToDisplay.filename() << endl
        << L"stem() = " << pathToDisplay.stem() << endl
        << L"extension() = " << pathToDisplay.extension() << endl;

    return wos.str();
}

int main()
{
    wcout << DisplayPathInfo() << endl;
    // wcout << ComparePaths() << endl; // see following example
    wcout << endl << L"Press Enter to exit" << endl;
    wstring input;
    getline(wcin, input);
}

このコードでは、次の出力が生成されます:

Displaying path info for: C:\FileSystemTest\SubDir3\SubDirLevel2\File2.txt
path part: 0 = C:
path part: 1 = \
path part: 2 = FileSystemTest
path part: 3 = SubDir3
path part: 4 = SubDirLevel2
path part: 5 = File2.txt
root_name() = C:
root_path() = C:\
relative_path() = FileSystemTest\SubDir3\SubDirLevel2\File2.txt
parent_path() = C:\FileSystemTest\SubDir3\SubDirLevel2
filename() = File2.txt
stem() = File2
extension() = .txt

パスの比較

path クラスは、 std::string および std::wstringと同じ比較演算子をオーバーロードします。 2 つのパスを比較する場合は、区切りが正規化された後で文字列を比較します。 末尾のスラッシュ (または円記号) が不足している場合は追加されず、比較に影響します。 パスの値を比較する方法を次の例に示します。

wstring ComparePaths()
{
    path p0(L"C:/Documents");                 // no trailing separator
    path p1(L"C:/Documents/");                // p0 < p1
    path p2(L"C:/Documents/2013/");           // p1 < p2
    path p3(L"C:/Documents/2013/Reports/");   // p2 < p3
    path p4(L"C:/Documents/2014/");           // p3 < p4
    path p5(L"D:/Documents/2013/Reports/");   // p4 < p5

    wostringstream wos;
    wos << boolalpha <<
        p0.wstring() << L" < " << p1.wstring() << L": " << (p0 < p1) << endl <<
        p1.wstring() << L" < " << p2.wstring() << L": " << (p1 < p2) << endl <<
        p2.wstring() << L" < " << p3.wstring() << L": " << (p2 < p3) << endl <<
        p3.wstring() << L" < " << p4.wstring() << L": " << (p3 < p4) << endl <<
        p4.wstring() << L" < " << p5.wstring() << L": " << (p4 < p5) << endl;
    return wos.str();
}
C:\Documents < C:\Documents\: true
C:\Documents\ < C:\Documents\2013\: true
C:\Documents\2013\ < C:\Documents\2013\Reports\: true
C:\Documents\2013\Reports\ < C:\Documents\2014\: true
C:\Documents\2014\ < D:\Documents\2013\Reports\: true

このコードを実行するには、上記の完全なサンプル コードに貼り付けて、main でこれを呼び出す行をコメント解除します。

パスと文字列型の間の変換

path オブジェクトは std::wstring または std::stringに暗黙的に変換できます。 つまり、次の例に示すように、wofstream::open などの関数にパスを渡すことができます。

// filesystem_path_conversion.cpp
// compile by using: /EHsc /W4 /permissive- /std:c++17 (or later)
#include <string>
#include <iostream>
#include <fstream>
#include <filesystem>

using namespace std;
using namespace std::filesystem;

int main()
{
    const wchar_t* p{ L"C:/Users/Public/Documents" };
    path filePath(p);

    filePath /= L"NewFile.txt";

    // Open, write to, and close the file.
    wofstream writeFile(filePath, ios::out);  // implicit conversion
    writeFile << L"Lorem ipsum\nDolor sit amet";
    writeFile.close();

    // Open, read, and close the file.
    wifstream readFile;
    wstring line;
    readFile.open(filePath);  // implicit conversions
    wcout << L"File " << filePath << L" contains:" << endl;
    while (readFile.good())
    {
        getline(readFile, line);
        wcout << line << endl;
    }
    readFile.close();

    wcout << endl << L"Press Enter to exit" << endl;
    wstring input;
    getline(wcin, input);
}
File C:\Users\Public\Documents\NewFile.txt contains:
Lorem ipsum
Dolor sit amet

Press Enter to exit

ディレクトリとファイルを反復する

<filesystem> ヘッダーは、単一のディレクトリを反復処理する directory_iterator 型と、ディレクトリとそのサブディレクトリを再帰的に反復処理する recursive_directory_iterator クラスを提供します。 path オブジェクトを渡して反復子を構成した後、反復子はパス内の最初の directory_entry を指します。 既定のコンストラクターを呼び出すことで、終了反復子を作成します。

ディレクトリを反復処理する場合に、検出する可能性があるいくつかの種類の項目があります。 これらの項目には、ディレクトリ、ファイル、シンボリック リンク、ソケット ファイルなどが含まれます。 directory_iterator は、その項目を directory_entry オブジェクトとして返します。