- 论坛徽章:
- 0
|
#include
#include
int stat(const char* pathname,struct stat *buf);
int fstat(int filedes,struct stat *buf);
int lstat(const char *pathname,struct stat *buf);
三个函数的返回:若成功则为0,若出错则为-1
给定一个pathname,stat函数返回一个与此命名文件有关的信息结构,fstat函数获得已在描述符filedes上打开的文件的有关信息。lstat函数类似于stat,但是当命名的文件是一个符号连接时,lstat返回该符号连接的有关信息,而不是由该符号连接引用的文件的信息。
第二个参数是个指针,它指向一个我们应提供的结构。这些函数填写由buf指向的结构。该结构的实际定义可能随实现而有所不同,但其基本形式是:
struct stat{
mode_t st_mode; /*file tpye &mode (permissions)*/
ino_t st_ino; /*i=node number (serial number)*/
dev_t st_rdev; /*device number for special files*/
nlink_t st_nlink; /*number of links*/
uid_t st_uid; /*user id of owner*/
gid_t st_gid; /*group ID of owner*/
off_t st_size; /*size in bytes for regular files*/
time_t st_atime; /*time of last access*/
time_t st_mtime; /*time of last modification*/
time_t st_ctime; /*time of last file status change*/
long st_blksize; /*best I/O block size */
long st_blocks; /*number of 512-byte blocks allocated*/
};
注意,除最后两个以外,其他各成员都为基本系统数据类型。我们将说明此结构的每个成员以了解文件属性。
使用stat函数最多的可能是ls-l命令,用其可以获得有关一个文件的所有信息。
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/34065/showart_286521.html |
|