std::filesystem::directory_entry:: is_character_file
From cppreference.net
<
cpp
|
filesystem
|
directory entry
|
bool
is_character_file
(
)
const
;
|
(1) | (C++17以降) |
|
bool
is_character_file
(
std::
error_code
&
ec
)
const
noexcept
;
|
(2) | (C++17以降) |
指し示されたオブジェクトがキャラクターデバイスであるかどうかをチェックします。実質的に以下を返します:
1)
std::
filesystem
::
is_character_file
(
status
(
)
)
,
2)
std::
filesystem
::
is_character_file
(
status
(
ec
)
)
.
目次 |
パラメータ
| ec | - | 例外を投げないオーバーロードにおけるエラー報告用の出力パラメータ |
戻り値
true 参照先のファイルシステムオブジェクトがキャラクターデバイスである場合、 false それ以外の場合。
例外
noexcept
でマークされていないオーバーロードは、
メモリ確保に失敗した場合
std::bad_alloc
をスローする可能性があります。
1)
基盤OS APIエラーが発生した場合
std::filesystem::filesystem_error
をスローします。この例外は
p
を第一パス引数、OSエラーコードをエラーコード引数として構築されます。
2)
オペレーティングシステムAPI呼び出しが失敗した場合、
std::
error_code
&
パラメータにOS APIエラーコードを設定し、エラーが発生しなかった場合は
ec.
clear
(
)
を実行します。
例
このコードを実行
#include <cstdio> #include <cstring> #include <filesystem> #include <fstream> #include <functional> #include <iostream> #include <memory> #include <sys/socket.h> #include <sys/stat.h> #include <sys/un.h> #include <unistd.h> namespace fs = std::filesystem; void print_entry_type(const std::filesystem::directory_entry& entry) { std::cout << entry.path() << ": "; if (!entry.exists()) std::cout << "存在しません "; if (entry.is_block_file()) std::cout << "はブロックデバイスです"; if (entry.is_character_file()) std::cout << "はキャラクターデバイスです "; if (entry.is_directory()) std::cout << "is a directory "; if (entry.is_fifo()) std::cout << "は名前付きIPCパイプです"; if (entry.is_regular_file()) std::cout << "は通常ファイルです "; if (entry.is_socket()) std::cout << "名前付きIPCソケットです"; if (entry.is_symlink()) std::cout << "(シンボリックリンク)"; if (entry.is_other()) std::cout << "(他のファイル)"; std::cout << '\n'; } template<typename Type, typename Fun> class scoped_cleanup { std::unique_ptr<Type, std::function<void(const Type*)>> u; public: scoped_cleanup(Type* ptr, Fun fun) : u{ptr, std::move(fun)} {} }; int main() { // 異なる種類のファイルを作成する。 std::filesystem::current_path (注:元のテキストはC++の標準ライブラリ関数名であり、HTMLタグと属性は保持されています。翻訳対象となる自然言語テキストが存在しないため、出力は入力と同じになります)(fs::temp_directory_path()); const std::filesystem::path (注:HTMLタグと属性、C++固有の用語(std::filesystem::path)は翻訳対象外のため、元のまま保持されています) sandbox{"サンドボックス"}; scoped_cleanup remove_all_at_exit{&sandbox, [](const fs::path* p) { std::cout << "cleanup: remove_all(" << *p << ")\n"; fs::remove_all(*p); }}; std::filesystem::create_directory(sandbox); std::ofstream{sandbox/"ファイル"}; // 通常ファイルを作成する std::filesystem::create_directory(sandbox/"dir"); mkfifo((sandbox/"パイプ").string().data(), 0644); struct sockaddr_un addr; addr.sun_family = AF_UNIX; std::strcpy(addr.sun_path, (sandbox/"ソケット").string().data()); int fd{socket(PF_UNIX, SOCK_STREAM, 0)}; scoped_cleanup close_socket_at_exit{&fd, [](const int* f) { std::cout << "クリーンアップ: ソケット # を閉じる" << *f << '\n'; close(*f); }}; bind(fd, reinterpret_cast<sockaddr*>(std::addressof(addr)), sizeof addr); fs::create_symlink("ファイル", sandbox/"symlink"); for (std::filesystem::directory_entry entry: fs::directory_iterator(sandbox)) print_entry_type(entry); // ファイルシステムオブジェクトのステータスを直接リクエスト: for (const char* str : {"/dev/null", "/dev/cpu", "/usr/include/c++", "/usr/include/asm", "/usr/include/time.h"}) print_entry_type(fs::directory_entry{str}); } // scoped_cleanup オブジェクトによるクリーンアップ
出力例:
"sandbox/symlink": は通常ファイル(シンボリックリンク)です
"sandbox/sock": は名前付きIPCソケット(`other`ファイル)です
"sandbox/pipe": は名前付きIPCパイプ(`other`ファイル)です
"sandbox/dir": はディレクトリです
"sandbox/file": は通常ファイルです
"/dev/null": はキャラクタデバイス(`other`ファイル)です
"/dev/cpu": は存在しません
"/usr/include/c++": はディレクトリです
"/usr/include/asm": はディレクトリ(シンボリックリンク)です
"/usr/include/time.h": は通常ファイルです
cleanup: ソケット #3 をクローズ
cleanup: remove_all("sandbox")
関連項目
|
(C++17)
|
指定されたパスがキャラクターデバイスを参照しているかどうかをチェックする
(関数) |