C++17 ファイルおよびディレクトリ操作 <ファイルシステム>

目次

パス操作

ディレクトリトラバーサル

文書の検査と操作

要約する


ディレクトリ操作用に C++ を記述するたびに、通常はプラットフォームの SDK を調整します。特に win32 API は覚えるのが非常に難しいため、ドキュメントをチェックして、Python の os モジュールと同じくらい便利なライブラリがあるかどうかを確認します。 。

そこで私はファイルシステムを見つけましたが、それを使用することはありませんでした(私の第 6 版 C++ 入門書の最新標準では、C++11 のみが紹介されています)。

使用方法は次のように整理されています。参考までに

コンピューター上のファイルやディレクトリを操作する必要がある場合、<filesystem>C++17 標準ライブラリのヘッダー ファイルは便利なツールを提供します。これは、パス操作、ディレクトリ トラバーサル、ファイル検査、ファイル操作などを含む、ファイルとディレクトリの操作を処理するための一連のクラスと関数を提供します。<filesystem>この記事ではヘッダーファイルの使い方や機能について紹介します。

コンピューター上のファイルやディレクトリを操作する必要がある場合、<filesystem>C++17 標準ライブラリのヘッダー ファイルは便利なツールを提供します。これは、パス操作、ディレクトリ トラバーサル、ファイル検査、ファイル操作などを含む、ファイルとディレクトリの操作を処理するための一連のクラスと関数を提供します。<filesystem>この記事ではヘッダーファイルの使い方や機能について紹介します。

パス操作

<filesystem>ヘッダー ファイル内のクラスはpath、ファイルまたはディレクトリのパスを表すために使用されます。さまざまな演算子や関数を使用してパスを操作できます。

まず、/演算子を使用してパスを結合できます。例えば:

#include <filesystem>
#include <iostream>

namespace fs = std::filesystem;

int main() {
  fs::path p1 = "C:/Users/";
  fs::path p2 = "John/Documents/";
  fs::path p3 = p1 / p2;
  std::cout << p3 << std::endl;
  return 0;
}

上記のコードは"C:/Users/John/Documents"、2 つのパスを結合した結果である を出力します。

+=次の演算子を使用して、既存のパスにパスを追加することもできます。

#include <filesystem>
#include <iostream>

namespace fs = std::filesystem;

int main() {
  fs::path p1 = "C:/Users/";
  fs::path p2 = "John/Documents/";
  p1 += p2;
  std::cout << p1 << std::endl;
  return 0;
}

上記のコードは"C:/Users/John/Documents"、既存のパスにパスを追加した結果である を出力します。

2 つのパスを比較する必要がある場合は、==AND!=演算子を使用してそれらが等しいかどうかを比較できます。

#include <filesystem>
#include <iostream>

namespace fs = std::filesystem;

int main() {
  fs::path p1 = "C:/Users/John/Documents/";
  fs::path p2 = "C:/Users/Jane/Documents/";
  if (p1 == p2) {
    std::cout << "The paths are equal" << std::endl;
  } else {
    std::cout << "The paths are not equal" << std::endl;
  }
  return 0;
}

上記のコードは を出力します"The paths are not equal"。これは、2 つのパスが等しくないためです。

このクラスは、演算子に加えて、pathパスを操作するための便利な関数をいくつか提供します。たとえば、parent_path()関数を使用してパス内の親ディレクトリを取得できます。

#include <filesystem>
#include <iostream>

namespace fs = std::filesystem;

int main() {
  fs::path p = "C:/Users/John/Documents/report.txt";
  fs::path parent = p.parent_path();
  std::cout << parent << std::endl;
  return 0;
}

上記のコードは"C:/Users/John/Documents"、パス内の親ディレクトリである を出力します。

stem()関数を使用して、パス内のファイル名 (拡張子なし) を取得することもできます。

#include <filesystem>
#include <iostream>

namespace fs = std::filesystem;

int main() {
  fs::path p = "C:/Users/John/Documents/report.txt";
fs::path filename = p.stem();
std::cout << filename << std::endl;
return 0;
}

上記のコードは、ファイル名 (拡張子は含まれません) である `"report"` を出力します。

パス内の拡張子を取得する必要がある場合は、`extension()` 関数を使用できます。

#include <filesystem>
#include <iostream>

namespace fs = std::filesystem;

int main() {
  fs::path p = "C:/Users/John/Documents/report.txt";
  fs::path extension = p.extension();
  std::cout << extension << std::endl;
  return 0;
}

上記のコードは".txt"、ファイルの拡張子である を出力します。

ディレクトリトラバーサル

<filesystem>ヘッダー ファイルは、ディレクトリ内のファイルとサブディレクトリを反復処理する関数を提供します。directory_iteratorクラスを使用して、ディレクトリのすべてのコンテンツを反復処理できます。たとえば、次のコードは、"C:/Users/John/Documents"ディレクトリ内のすべてのファイルとサブディレクトリを反復処理します。

#include <filesystem>
#include <iostream>

namespace fs = std::filesystem;

int main() {
  fs::path p = "C:/Users/John/Documents";
  for (auto& entry : fs::directory_iterator(p)) {
    std::cout << entry.path() << std::endl;
  }
  return 0;
}

上記のコードは、ディレクトリ内のすべてのファイルとサブディレクトリのパスを出力します。

ディレクトリ内のファイルを反復処理するだけの場合は、is_regular_file()関数を使用してエントリが通常のファイルかどうかを判断できます。

#include <filesystem>
#include <iostream>

namespace fs = std::filesystem;

int main() {
  fs::path p = "C:/Users/John/Documents";
  for (auto& entry : fs::directory_iterator(p)) {
    if (fs::is_regular_file(entry)) {
      std::cout << entry.path() << std::endl;
    }
  }
  return 0;
}

上記のコードは、ディレクトリ内のすべてのファイルのパスを出力します。

ディレクトリ内のサブディレクトリのみを反復処理する場合は、is_directory()関数を使用してエントリがディレクトリであるかどうかを判断できます。

#include <filesystem>
#include <iostream>

namespace fs = std::filesystem;

int main() {
  fs::path p = "C:/Users/John/Documents";
  for (auto& entry : fs::directory_iterator(p)) {
    if (fs::is_directory(entry)) {
      std::cout << entry.path() << std::endl;
    }
  }
  return 0;
}

上記のコードは、ディレクトリ内のすべてのサブディレクトリのパスを出力します。

C++17 の<filesystem>標準ライブラリには、現在の作業ディレクトリを取得し、ディレクトリを作成する関数が用意されています。

現在の作業ディレクトリを取得するstd::filesystem::current_path方法、std::filesystem::path現在の作業ディレクトリのパスを表す type のオブジェクトを返す関数を使用することです。例えば:

#include <filesystem>
#include <iostream>

int main() {
    std::filesystem::path currentPath = std::filesystem::current_path();
    std::cout << "Current working directory is: " << currentPath << std::endl;

    return 0;
}

ディレクトリを作成するには、std::filesystem::create_directory関数。この関数はstd::filesystem::path、作成するディレクトリのパスを表す type のオブジェクトを受け取ります。例えば:

#include <filesystem>
#include <iostream>

int main() {
    std::filesystem::path dirPath = "C:/mydir";
    if (std::filesystem::create_directory(dirPath)) {
        std::cout << "Directory created successfully." << std::endl;
    } else {
        std::cout << "Failed to create directory." << std::endl;
    }

    return 0;
}

文書の検査と操作

<filesystem>ヘッダー ファイル内の<fstream>およびヘッダー ファイルは<iostream>、ファイルを調べて操作するための機能を提供します。たとえば、exists()関数を使用してファイルが存在するかどうかを確認できます。

#include <filesystem>
#include <iostream>

namespace fs = std::filesystem;

int main() {
  fs::path p = "C:/Users/John/Documents/report.txt";
  if (fs::exists(p)) {
    std::cout << "The file exists" << std::endl;
  } else {
    std::cout << "The file does not exist" << std::endl;
  }
  return 0;
}

"The file exists"ファイルが存在する場合、上記のコードは出力します。

ファイルを削除するには関数を使用できますremove()

#include <filesystem>
#include <iostream>

namespace fs = std::filesystem;

int main() {
  fs::path p = "C:/Users/John/Documents/report.txt";
  if (fs::exists(p)) {
    fs::remove(p);
    std::cout << "The file was successfully deleted" << std::endl;
  } else {
    std::cout << "The file does not exist" << std::endl;
  }
  return 0;
}

上記のコードはファイルを削除し、"The file was successfully deleted"ファイルが存在する場合は出力します。

関数を使用してファイルをコピーすることもできますcopy()

#include <filesystem>
#include <iostream>

namespace fs = std::filesystem;

int main() {
  fs::path source = "C:/Users/John/Documents/report.txt";
  fs::path target = "C:/Users/John/Documents/report_copy.txt";
  if (fs::exists(source)) {
    fs::copy(source, target);
    std::cout << "The file was successfully copied" << std::endl;
  } else {
    std::cout << "The file does not exist" << std::endl;
  }
  return 0;
}

"The file was successfully copied"上記のコードは、ソース ファイルが存在する場合、ファイルをコピーして出力します。

関数を使用してファイルの名前を変更することもできますrename()

#include <filesystem>
#include <iostream>

namespace fs = std::filesystem;

int main() {
  fs::path old_path = "C:/Users/John/Documents/report.txt";
  fs::path new_path = "C:/Users/John/Documents/report_renamed.txt";
  if (fs::exists(old_path)) {
    fs::rename(old_path, new_path);
    std::cout << "The file was successfully renamed" << std::endl;
  } else {
    std::cout << "The file does not exist" << std::endl;
  }
  return 0;
}

"The file was successfully renamed"元のファイルが存在する場合、上記のコードはファイルの名前を変更して出力します。

要約する

<filesystem>ヘッダー ファイルは、ファイル システムを操作するための強力なツール セットを提供します。これを使用して、ファイルとディレクトリを管理し、ファイルとディレクトリに関する情報を取得し、ファイル操作を実行できます。<filesystem>これらの機能により、ヘッダー ファイルは複数のプラットフォームで使用できるため、ファイルとディレクトリの操作がより簡単かつ移植可能になります。

おすすめ

転載: blog.csdn.net/weixin_40582034/article/details/129323112