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);
  • 相关阅读:
    数据库函数(备忘)
    C语言实现统计字符个数
    用链表做一个学生管理系统
    深入理解 JSON
    从 JDK 源码角度看 Object
    eclipse + maven搭建SSM框架
    Oracle12c版64位客户端安装步骤(32位安装步骤一样)
    Oracle12C版本安装步骤
    ArcGIS破解配置及oracle文件配置
    ArcGIS_Lisence安装步骤
  • 原文地址:https://www.cnblogs.com/embedded-linux/p/5046530.html
Copyright © 2011-2022 走看看