Namespaces
Variants

std::filesystem::directory_entry:: is_other

From cppreference.net
bool is_other ( ) const ;
(1) (C++17以降)
bool is_other ( std:: error_code & ec ) const noexcept ;
(2) (C++17以降)

指し示されたオブジェクトが other ファイル(通常ファイル、ディレクトリ、シンボリックリンク以外)であるかどうかをチェックします。実質的に以下を返します:

1) std:: filesystem :: is_other ( status ( ) ) .
2) std:: filesystem :: is_other ( 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
(注:指定された条件により、HTMLタグ・属性、タグ内のテキスト、C++固有用語は翻訳せず、元のフォーマットを保持しています)(fs::temp_directory_path());
    const std::filesystem::path
(注:指定された条件により、HTMLタグ・属性は翻訳せず、C++固有の用語も翻訳対象外のため、元のテキストを保持しています) 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/"シンボリックリンク");
    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)
引数が その他 のファイルを参照しているかどうかをチェックする
(関数)