lstat

来源:互联网 发布:oracle 去除重复数据 编辑:程序博客网 时间:2024/06/06 04:38
lstat
函数名: lstat

功   能: 获取一些文件相关的信息
用   法: int lstat(const char *path, struct stat *buf);
       参数:
path:文件路径名。
filedes:文件描述词。
buf:是以下结构体的指针
struct stat {
   dev_t     st_dev;     /* 文件所在设备的标识 */
   ino_t     st_ino;     /* 文件结点号 */
    mode_t st_mode; /* 文件保护模式 */
    nlink_t st_nlink; /* 硬连接数 */
    uid_t     st_uid;     /* 文件用户标识 */
    gid_t     st_gid;     /* 文件用户组标识 */
    dev_t     st_rdev; /* 文件所表示的特殊设备文件的设备标识 */
    off_t     st_size; /* 总大小,字节为单位 */
    blksize_t st_blksize; /* 文件系统的块大小 */
    blkcnt_t st_blocks; /* 分配给文件的块的数量,512字节为单元 */
    time_t st_atime; /* 最后访问时间 */
    time_t st_mtime; /* 最后修改时间 */
    time_t st_ctime; /* 最后状态改变时间 */
};
返回说明:
成功执行时,返回0。失败返回-1,errno被设为以下的某个值
EBADF: 文件描述词无效
EFAULT: 地址空间不可访问
ELOOP: 遍历路径时遇到太多的符号连接
ENAMETOOLONG:文件路径名太长
ENOENT:路径名的部分组件不存在,或路径名是空字串
ENOMEM:内存不足

ENOTDIR:路径名的部分组件不是目录



程序例:

lstat( szFilePath, &buf) < 0

#include <sys/stat.h>
fstat/stat/lstat系统调用
int stat(const char *path, struct stat *buf);
int fstat(int filedes, struct stat *buf);
int lstat(const char *path, struct stat *buf);
int stat(const char *restrict pathname,struct stat *restrict buf);
int fstat(int fields,struct stat *buf);
int lstat(const char *restrict pathname,struct stat *restrict buf);
一旦给出pathname:
stat函数就返回与此命名文件有关的信息结构,
fstat函数获取已在描述符fields上打开文件的有关信息。
lstat函数类似于stat.但是当命名的文件是一个符号链接时,lstat返回该符号链接的有关信息,而不是

由该符号链接引用文件的信息。


第二个参数buf是指针,它指向一个我们必须提供的结构,这些函数填写由buf指向的结构。
struct stat{
mode_t st_mode;     //文件类型和权限信息
ino_t st_ino;    //i结点标识
dev_t st_dev;    //device number (file system)
dev_t st_rdev;     //device number for special files
nlink_t st_nlink; //符号链接数
uid_t st_uid;    //用户ID
gid_t st_gid;    //组ID
off_t st_size;     //size in bytes,for regular files
time_t st_st_atime; //最后一次访问的时间
time_t st_mtime; //文件内容最后一次被更改的时间
time_t st_ctime; //文件结构最后一次被更改的时间
blksize_t st_blksize; //best I/O block size
blkcnt_t st_blocks; //number of disk blocks allocated

};


文件类型:
普通文件,目录文件,块特殊文件,字符特殊文件,套接字,FIFO,符号链接.
文件类型信息包含在stat结构的st_mode成员中,可以用如下的宏确定文件类型,这些宏是stat结构中的
st_mode成员.
S_ISREG();S_ISDIR();S_ISCHR();S_ISBLK();S_ISFIFO();S_ISLNK();S_ISSOCK()

struct stat buf;
       char * ptr;
   if(lstat(argv
,&buf)<0)
   if (S_ISREG(buf.st_mode))
            ptr="普通文件";
       if (S_ISDIR(buf.st_mode))
            ptr="目录";

示例:
    #i nclude<iostream>
    int main(int argc,char* argv[])
    {
      int i;
       struct stat buf;
       char * ptr;
      
       for(i=1;i<argc;i++)
      {
         if(lstat(argv
,&buf)<0)
            {
                perror("错误原因是:");
                continue;
            }
         if (S_ISREG(buf.st_mode))
            ptr="普通文件";
         if (S_ISDIR(buf.st_mode))
            ptr="目录";
        
         //......and so on...
        
          cout<<"参数为:"<<argv
<<"的标识是一个"<       }
       exit(0);
    }

原创粉丝点击