c/c++语言函数 stat, fstat, lstat, fstatat - get file status

前言全局说明

c/c++语言函数 stat, fstat, lstat, fstatat - get file status


一、说明


二、函数原型

2.1

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int stat(const char *pathname, struct stat *statbuf);
int fstat(int fd, struct stat *statbuf);
int lstat(const char *pathname, struct stat *statbuf);

2.2

#include <fcntl.h> /* Definition of AT_* constants */
#include <sys/stat.h>
int fstatat(int dirfd, const char *pathname, struct stat *statbuf, int flags);

2.3

struct stat {
dev_t st_dev; /* ID of device containing file */
ino_t st_ino; /* Inode number */
mode_t st_mode; /* File type and mode */
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; /* Block size for filesystem I/O */
blkcnt_t st_blocks; /* Number of 512B blocks allocated */
/* Since Linux 2.6, the kernel supports nanosecond
precision for the following timestamp fields.
For the details before Linux 2.6, see NOTES. */
struct timespec st_atim; /* Time of last access */
struct timespec st_mtim; /* Time of last modification */
struct timespec st_ctim; /* Time of last status change */
#define st_atime st_atim.tv_sec /* Backward compatibility */
#define st_mtime st_mtim.tv_sec
#define st_ctime st_ctim.tv_sec
};

三、使用示例

3.1 获取文件大小

https://www.cnblogs.com/wutou/p/18268849

3.2

文件名:


四、参数,速查表格

4.1 struct stat结构体

常量 权限 中文说明 原文说明 备注
st_dev This field describes the device on which this file resides. (The major(3) and minor(3) macros may be useful to decompose the device ID in this field.)
st_ino This field contains the file's inode number.
st_mode This field contains the file type and mode. See inode(7) for further information.
st_nlink This field contains the number of hard links to the file.
st_uid This field contains the user ID of the owner of the file.
st_gid This field contains the ID of the group owner of the file.
st_rdev This field describes the device that this file (inode) represents.
st_size 获取文件大小 This field gives the size of the file (if it is a regular file or a symbolic link) in bytes. The size of a symbolic link is the length of the pathname it contains, without a terminating null byte.
st_blksize This field gives the "preferred" block size for efficient filesystem I/O.
st_blocks This field indicates the number of blocks allocated to the file, in 512-byte units. (This may be smaller than st_size/512 when the file has holes.)
st_atime This is the file's last access timestamp.
st_mtime This is the file's last modification timestamp.
st_ctime This is the file's last status change timestamp.

4.2 错误返回值

常量 权限 中文说明 原文说明 备注
EACCES Search permission is denied for one of the directories in the path prefix of pathname. (See also path_resolution(7).)
EBADF fd is not a valid open file descriptor.
EFAULT Bad address.
ELOOP Too many symbolic links encountered while traversing the path.
ENAMETOOLONG pathname is too long.
ENOENT A component of pathname does not exist, or pathname is an empty string and AT_EMPTY_PATH was not specified in flags.
ENOMEM Out of memory (i.e., kernel memory).
ENOTDIR A component of the path prefix of pathname is not a directory.
EOVERFLOW pathname or fd refers to a file whose size, inode number, or number of blocks cannot be represented in, respectively, the types off_t,ino_t, or blkcnt_t. This error can occur when, for example, an application compiled on a 32-bit platform without -D_FILE_OFF‐SET_BITS=64 calls stat() on a file whose size exceeds (1<<31)-1 bytes. The following additional errors can occur for fstatat():
EBADF dirfd is not a valid file descriptor.
EINVAL Invalid flag specified in flags.
ENOTDIR pathname is relative and dirfd is a file descriptor referring to a file other than a directory.


免责声明:本号所涉及内容仅供安全研究与教学使用,如出现其他风险,后果自负。




参考、来源:
man 2 fstat
https://blog.csdn.net/weixin_37926485/article/details/122804385



posted @   悟透  阅读(122)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2023-06-26 read: arg count
点击右上角即可分享
微信分享提示