zoukankan      html  css  js  c++  java
  • 系统文件操作进一步

    file_advance

    • access

    int access(cosnt char *pathname, int mode);

    mode: R_OK | W_OK | X_OK 或 F_OK测试文件存在性。

    F_OK tests for the existence of the file. R_OK, W_OK, and X_OK test whether the file exists and grants read, write, and execute permissions, respectively.

    • stat
    #include <sys/stat.h>
    #include <unistd.h>
    
    
    int stat(const char *file_name, struct stat *buf);
    
    
    int fstat(int fds, struct stat *buf);
    
    
    struct stat {
    dev_t st_dev; /* 设备 */
    ino_t st_ino; /* 节点 */
    mode_t st_mode; /* 模式 */
    nlink_t st_nlink; /* 硬连接 */
    uid_t st_uid; /* 用户 ID */
    gid_t st_gid; /* 组 ID */
    dev_t st_rdev; /* 设备类型 */
    off_t st_off; /* 文件字节数 */
    unsigned long st_blksize; /* 块大小 */
    unsigned long st_blocks; /* 块数 */
    time_t st_atime; /* 最后一次访问时间 */
    time_t st_mtime; /* 最后一次修改时间 */
    time_t st_ctime; /* 最后一次改变时间(指属性) */
    
    

    };

    stat用来判断没有打开的文件,而fstat用来判断打开的文件。使用最多的属性是st_mode,用下面的宏判断:

    S_ISLNK(st_mode):是否是一个连接;

    S_ISREG(st_mode):是否是一个常规文件;

    S_ISDIR(st_mode):是否是一个目录;

    S_ISCHR(st_mode):是否是一个字符设备;

    S_ISBLK(st_mode):是否是一个块文件;

    S_ISFIFO(st_mode):是否是FIFO文件;

    S_ISSOCK(st_mode):是否是SOCKET文件。

    注:stat也用来判断文件是否存在。

    static int create_open_file(int * fd,char * name)
    {
        int err;
        struct stat file_status;
        if(stat(name, &file_status) < 0)//文件不存在    
        {        
            *fd = open(name, O_WRONLY | O_CREAT,0755);    
            if(*fd < 0)            
            {            
                return -1;        
            }        
        }    
        else    
        {        
            *fd = open(name, O_WRONLY|O_APPEND ,0755);    
            if(*fd < 0)        
            {            
                return -1;        
            }    
        }
            
        if(lseek(*fd,0, SEEK_END) < 0)//定位到文件尾端    
        {    
            close(*fd);        
            return -1;
        }    
        return 0;    
    }     
    • 目录操作
    char *getcwd(char *buffer, size_t size);
    
    
    int mkdir(const char *path, mode_t mode);
    
    
    DIR *opendir(const char *path);
    
    
    struct dirent *readdir(DIR *dir);
    
    
    void rewinddir(DIR *dir);
    
    
    void seekdir(DIR *dir, off_t off);
    
    
    int closedir(DIR *dir);
  • 相关阅读:
    linux磁盘简介
    linux用户管理、su和sudo、文件权限、SUID/SGID/SBIT
    dubbo框架的使用方法。。。
    Linux 配置 dubbo 和 dubbo的简单介绍。
    单列模式简单介绍
    Linux配置zookeeper 和zookeeper简单介绍
    Linux配置 ftp 和 ftp简单介绍
    正向代理 、反向代理, 和 Linux系统配置nginx。
    Linux配置Redis集群 和 缓存介绍。
    Linux 集群 和免秘钥登录的方法。
  • 原文地址:https://www.cnblogs.com/embedded-linux/p/5046530.html
Copyright © 2011-2022 走看看