Unix环境高级编程笔记:4、文件和目录

1、stat   fstat   lstat
    #include <sys/stat.h>
    int stat(const char * restrict pathname,struct stat *restrict buf);
    int fstat(int filedes,struct stat *buf);
    int lstat(const char *restrict pathname,struct stat *restrict buf);
 
        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 file system I/O */
               blkcnt_t  st_blocks;  /* number of 512B blocks allocated */
               time_t    st_atime;   /* time of last access */
               time_t    st_mtime;   /* time of last modification */
               time_t    st_ctime;   /* time of last status change */
           };
 
2、文件类型
    文件类型定义在stat结构的st_mode成员中,宏确定文件类型,这些宏的参数都是stat结构中的st_mode成员
 
           S_ISREG(m)  is it a regular file?
 
           S_ISDIR(m)  directory?
 
           S_ISCHR(m)  character device?
 
           S_ISBLK(m)  block device?
 
           S_ISFIFO(m) FIFO (named pipe)?
 
           S_ISLNK(m)  symbolic link? (Not in POSIX.1-1996.)
 
           S_ISSOCK(m) socket? (Not in POSIX.1-1996.)
 
3、chmod fchmod
4、chown、fchown、lchown
5、文件长度
    stat结构成员st_size 表示以字节为单位的文件长度
 
6、文件截短
    #include <unistd.h>
    int truncate(const char *pathname,off_t length);
 
7、link、unlink、remove、rename
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

猜你喜欢

转载自hackxin.iteye.com/blog/1965962