浅学C++(6)Linux环境编程(文件操作)

文件同步:
    1,在写入数据时,内存与磁盘之间有一块缓冲区,这种机制降低了磁盘读写次数、提高了读写效率
    2,但是这种机制带来的后果是磁盘的数据域实际的数据不匹配,系统提供了三个系统函数可以让缓冲区的数据立即写入到磁盘
    
    void sync(void);
    功能:立即把缓冲区种的数据同步到磁盘
    tap:并不会等待数据同步结束才返回,而是提交要同步的数据写入队列中,就返回。

    int fsync(int fd);
    功能:把指定文件的内容同步到磁盘
    tap:会等到完全写入次盘后才返回

    int fdatasync(int fd);
    功能:把指定文件的内容同步到磁盘,只同步文件内容不同步文件属性

文件属性
    #include 
    #include 
    #include 

    int stat(const char *pathname, struct stat *buf);
    功能:根据文件的路径获取文件属性
    buf:存储文件属性的结构体 是一个输出型参数
    int fstat(int fd, struct stat *buf);
    功能:根据文件描述符获取文件属性
    int lstat(const char *pathname, struct stat *buf);
    功能:根据文件的路径获取软链接文件属性

    struct stat 
    {
        dev_t     st_dev;         /* ID of device containing file */    设备ID
        ino_t     st_ino;         /* inode number */                    inode节点号
        mode_t    st_mode;        /* protection */                      文件类型和权限
        nlink_t   st_nlink;       /* number of hard links */            硬连接数
        uid_t     st_uid;         /* user ID of owner */                用户ID
        gid_t     st_gid;         /* group ID of owner */               组ID
        dev_t     st_rdev;        /* device ID (if special file) */     特殊设备ID号
        off_t     st_size;        /* total size, in bytes */            总字节数
        blksize_t st_blksize;     /* blocksize for filesystem I/O */    IO块字节数
        blkcnt_t  st_blocks;      /* number of 512B blocks

你可能感兴趣的:(linux,c++)