Header file: #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> DEFINITION: int stat(const char *pathname, struct stat *buf); DESCRIPTIO ...
Header file:
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
DEFINITION:
int stat(const char *pathname, struct stat *buf);
DESCRIPTION:
The functions return information about a file, in the buffer pointed to by buf.
No permissions are required on the file itself, but—in the case of stat(), fstatat(), and
lstat()—execute (search) permission is required on all of the directories in pathname
that lead to the file.
stat() and fstatat() retrieve information about the file pointed to by pathname;
stat() and fstatat() retrieve information about the file pointed to by pathname; the differences for fstatat() are described below.
lstat() is identical to stat(), except that if pathname is a symbolic link, then it returns information about the link itself, not the file that it refers to.
fstat() is identical to stat(), except that the file about which information is to be retrieved is specified by the file descriptor fd.
RETURN VALUE:
On success, zero is returned. On error, -1 is returned, and errno is set appropriately.
ERRORS:
EACCES Search permission is denied for one of the directories in the path prefix of path‐name. (See also path_resolution(7).)
EBADF fd is bad.
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.
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 can‐not 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_OFFSET_BITS=64 calls stat() on a file whose size exceeds (1<<31)-1 bytes.
#include <sys/stat.h> #include <unistd.h> #include <stdio.h> int main() { struct stat buf; stat("HOME/C++/test", &buf); printf("the file size = %d\n", buf.st_size); }
stat structure:
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 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};
};
fstat(int fd,struct stat *)接收的已open的文件描述符
stat(char *filename,struct stat *)接收的路徑名, 需要註意的是 能處理符號鏈接,但處理的是符號鏈接指向的文件。
lstat(char *filename,struct stat *)接收的路徑名 ,需要註意的是,也能能處理符號鏈接,但處理的是符號鏈接本身(自身)文件。