在Linux中,可以利用stat()函数来获取一个文件的状态
-
#include <sys/stat.h>
-
#include <unistd.h>
-
-
int stat(const char *file_name, struct stat *buf);
这个函数执行成功返回0,失败返回-1。取得的文件状态存放在buf指针指向的struct stat结构提中 .
转::http://blog.chinaunix.net/uid-25958655-id-4365384.html
原文: http://blog.csdn.net/simmerlee/article/details/8281399
在不同的linux和unix中, struct stat具体声明时,里面的成员顺序不一定相同与
中说的一模一样.
让我们去查找一下, 在/usr/include/sys/stat.h中没找到struct stat 定义, 只能去它所包含的头文中去找,它包含了这几个头文件,#include,#include,#include,#include
RedHat linux6.4 中(其它RedHat和Fedora估计类似),struct stat 的定义是在/usr/include/bits/stat.h 头文件中声明形式如下:
-
struct stat
-
{
-
__dev_t st_dev; /* Device. */
-
unsigned short int __pad1;
-
#ifndef __USE_FILE_OFFSET64
-
__ino_t st_ino; /* File serial number. */
-
#else
-
__ino_t __st_ino; /* 32bit file serial number. */
-
#endif
-
__mode_t st_mode; /* File mode. */
-
__nlink_t st_nlink; /* Link count. */
-
__uid_t st_uid; /* User ID of the file's owner. */
-
__gid_t st_gid; /* Group ID of the file's group.*/
-
__dev_t st_rdev; /* Device number, if device. */
-
unsigned short int __pad2;
-
#ifndef __USE_FILE_OFFSET64
-
__off_t st_size; /* Size of file, in bytes. */
-
#else
-
__off64_t st_size; /* Size of file, in bytes. */
-
#endif
-
__blksize_t st_blksize; /* Optimal block size for I/O. */
-
-
#ifndef __USE_FILE_OFFSET64
-
__blkcnt_t st_blocks; /* Number 512-byte blocks allocated. */
-
#else
-
__blkcnt64_t st_blocks; /* Number 512-byte blocks allocated. */
-
#endif
-
#if defined __USE_MISC || defined __USE_XOPEN2K8
-
/* Nanosecond resolution timestamps are stored in a format
-
equivalent to 'struct timespec'. This is the type used
-
whenever possible but the Unix namespace rules do not allow the
-
identifier 'timespec' to appear in the <sys/stat.h> header.
-
Therefore we have to handle the use of this header in strictly
-
standard-compliant sources special. */
-
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
-
#else
-
__time_t st_atime; /* Time of last access. */
-
unsigned long int st_atimensec; /* Nscecs of last access. */
-
__time_t st_mtime; /* Time of last modification. */
-
unsigned long int st_mtimensec; /* Nsecs of last modification. */
-
__time_t st_ctime; /* Time of last status change. */
-
unsigned long int st_ctimensec; /* Nsecs of last status change. */
-
#endif
-
#ifndef __USE_FILE_OFFSET64
-
unsigned long int __unused4;
-
unsigned long int __unused5;
-
#else
-
__ino64_t st_ino; /* File serial number. */
-
#endif
-
-
};
而mode_t呢, 从 "usr/include/sys/stat.h"中的 一行"#include /* For __mode_t and __dev_t. */ " 就可以知道__mode_t应该是在/usr/include/sys/bits/types.h中
当然如果你只是想知道 stat中每一种成员类型占用了几个字节的话, 只要sizeof(类型)就好拉, 如printf("%d",sizeof(dev_t));
还有这个另外一篇: http://blog.chinaunix.net/uid-25958655-id-4365384.html
阅读(3595) | 评论(0) | 转发(0) |