WIN32_FIND_DATA equivalent in Linux C++

WIN32_FIND_DATA equivalent in Linux C++

本文关键字:Linux C++ in DATA FIND WIN32 equivalent      更新时间:2023-10-16

Linux C++ 中WIN32_FIND_DATA的等价物是什么?

WIN32_FIND_DATA fileInfo;

WIN32_FIND_DATA是 Windows 规范的数据类型。

当我用 C++11 切换到 Linux Centos 7 时,我需要找到与它等效的,因为WIN32_FIND_DATA有几种方法在 Linux 中不支持。

fileInfo.cFileName

C++17 有filesystem

例:

#include <filesystem>
namespace fs = std::filesystem;
int main()
{
    fs::path p { "/usr/lib/" };
    for (auto& entry : p)
    {
        // do something with entry
    }
    return 0;
}

它基于 Boost 库中的文件系统功能,因此您可以将其与较旧的编译器一起使用。

定义为:

(最接近您需要的)结构stat结构体

struct stat {
    dev_t     st_dev;     /* ID of device containing file */
    ino_t     st_ino;     /* inode number */
    mode_t    st_mode;    /* protection */
    nlink_t   st_nlink;   /* number of hard links */
    uid_t     st_uid;     /* user ID of owner */
    gid_t     st_gid;     /* group ID of owner */
    dev_t     st_rdev;    /* device ID (if special file) */
    off_t     st_size;    /* total size, in bytes */
    blksize_t st_blksize; /* blocksize for file system I/O */
    blkcnt_t  st_blocks;  /* number of 512B blocks allocated */
    time_t    st_atime;   /* time of last access */
    time_t    st_mtime;   /* time of last modification */
    time_t    st_ctime;   /* time of last status change */
};

否则你必须从头开始构建它,GNU Core Utils可以提供帮助。