如何在Linux中读取文件/文件夹属性?

时间:2023-02-05 13:10:22

How to retrieve a file/folder's properties in C, particularly in Linux?

如何在C中检索文件/文件夹的属性,特别是在Linux中?

I need info about date created, last modified, isDirectory or isFile, permission, ownership and size.

我需要有关创建日期,上次修改日期,isDirectory或isFile,权限,所有权和大小的信息。

Thanks.

3 个解决方案

#1


4  

You most likely need the stat() function.

您最有可能需要stat()函数。

Example:

struct stat attr;
stat("/home/crazyfffan/foo.txt", &attr);

printf("Size: %u\n", (unsigned)attr.st_size);
printf("Permissions: %o\n", (int)attr.st_mode & 07777);
printf("Is directory? %d\n", attr.st_mode & ST_ISDIR);

etc.

#2


3  

Use the stat system call. man 2 stat.

使用stat系统调用。男人2 stat。

You'll get a structure that includes what you're looking for.

你会得到一个包含你正在寻找的东西的结构。

From the man page:

从手册页:

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 */
       };

Look over the example in the man page for details about determining file type using the st_mode field; here's how to check isDirectory/isFile using the POSIX macros:

有关使用st_mode字段确定文件类型的详细信息,请查看手册页中的示例;这里是如何使用POSIX宏检查isDirectory / isFile:

isDirectory = S_ISDIR(statBuf.st_mode);
isFile = S_ISREG(statBuf.st_mode);

#3


1  

struct stat file_stats;    

fd = open(filename, O_RDONLY);
if (fd == -1) {
    exit(-1);
}

if (fstat(fd, &file_stats) < 0) {
    exit(-1);
}
if (S_ISDIR(file_stats.st_mode)) {
      printf("It is dir\n");
} else {
    snprintf(msg, PATH_MAX, "%lld, %ld, %o, %d, %d, %d, %lld, %ld, %ld, %ld, %ld, %ld,
    %ld\n",
            file_stats.st_dev,
            file_stats.st_ino,
            file_stats.st_mode,
            file_stats.st_nlink,
            file_stats.st_uid,
            file_stats.st_gid,
            file_stats.st_rdev,
            file_stats.st_size,
            file_stats.st_blksize,
            file_stats.st_blocks,
            file_stats.st_atime,
            file_stats.st_mtime,
            file_stats.st_ctime);
}

#1


4  

You most likely need the stat() function.

您最有可能需要stat()函数。

Example:

struct stat attr;
stat("/home/crazyfffan/foo.txt", &attr);

printf("Size: %u\n", (unsigned)attr.st_size);
printf("Permissions: %o\n", (int)attr.st_mode & 07777);
printf("Is directory? %d\n", attr.st_mode & ST_ISDIR);

etc.

#2


3  

Use the stat system call. man 2 stat.

使用stat系统调用。男人2 stat。

You'll get a structure that includes what you're looking for.

你会得到一个包含你正在寻找的东西的结构。

From the man page:

从手册页:

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 */
       };

Look over the example in the man page for details about determining file type using the st_mode field; here's how to check isDirectory/isFile using the POSIX macros:

有关使用st_mode字段确定文件类型的详细信息,请查看手册页中的示例;这里是如何使用POSIX宏检查isDirectory / isFile:

isDirectory = S_ISDIR(statBuf.st_mode);
isFile = S_ISREG(statBuf.st_mode);

#3


1  

struct stat file_stats;    

fd = open(filename, O_RDONLY);
if (fd == -1) {
    exit(-1);
}

if (fstat(fd, &file_stats) < 0) {
    exit(-1);
}
if (S_ISDIR(file_stats.st_mode)) {
      printf("It is dir\n");
} else {
    snprintf(msg, PATH_MAX, "%lld, %ld, %o, %d, %d, %d, %lld, %ld, %ld, %ld, %ld, %ld,
    %ld\n",
            file_stats.st_dev,
            file_stats.st_ino,
            file_stats.st_mode,
            file_stats.st_nlink,
            file_stats.st_uid,
            file_stats.st_gid,
            file_stats.st_rdev,
            file_stats.st_size,
            file_stats.st_blksize,
            file_stats.st_blocks,
            file_stats.st_atime,
            file_stats.st_mtime,
            file_stats.st_ctime);
}