• Linux 文件读写


    函数的详细介绍可以用man命令来查看,例如 man 2 open.

    open

    #include 
    
    int open(const char *pathname, int flags);
    int open(const char *pathname, int flags, mode_t mode);
    
    • 1
    • 2
    • 3
    • 4

    The open() system call opens the file specified by pathname. If the specified file does not exist, it may optionally (if O_CREAT is specified in flags) be created by open().

    The return value of open() is a file descriptor, a small, nonnegative integer that is used in subsequent system calls (read(2), write(2), lseek(2), fcntl(2), etc.) to refer to the open file.The file descriptor returned by a successful call will be the lowest-numbered file descriptor not currently open for the process.

    lseek

    #include 
    #include 
    
    off_t lseek(int fd, off_t offset, int whence);
    
    • 1
    • 2
    • 3
    • 4

    DESCRIPTION
    lseek() repositions the file offset of the open file description associated with the file descriptor fd to the argument offset according to the directive whence as follows:

       SEEK_SET
              The file offset is set to offset bytes.
    
       SEEK_CUR
              The file offset is set to its current location plus offset bytes.
    
       SEEK_END
              The file offset is set to the size of the file plus offset bytes.
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    lseek() allows the file offset to be set beyond the end of the file (but this does not change the size of the file). If data is later written at this point, subsequent reads of the data in the gap (a “hole”) return null bytes (‘\0’) until data is actually written into the gap.

    write

    #include 
    
    ssize_t write(int fd, const void *buf, size_t count);
    
    • 1
    • 2
    • 3

    DESCRIPTION
    write() writes up to count bytes from the buffer starting at buf to the file referred to by the file descriptor fd.

    read

    #include 
    
    ssize_t read(int fd, void *buf, size_t count);
    
    • 1
    • 2
    • 3

    DESCRIPTION
    read() attempts to read up to count bytes from file descriptor fd into the buffer starting at buf.

    On files that support seeking, the read operation commences at the file offset, and the file offset is incremented by the number of bytes read. If the file offset is at or past the end of file, no bytes are read, and read() returns zero.

    示例

    #include 
    #include 
    #include 
    #include 
    
    
    int main(){
        int fd;
        int write_data = 10;
        int read_data;
        fd = open("file",O_RDWR | O_CREAT,0644);  //打开文件,若不存在就创建,权限:可读可写+可读+可读
        lseek(fd,SEEK_SET,0);   //设置写位置
        write(fd,(char*)&write_data,sizeof(write_data));  //写入数据
        lseek(fd,SEEK_SET,0);   //设置读位置
        read(fd,(char*)&read_data,sizeof(read_data));   //读取数据
        printf("%d\r\n",read_data);
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
  • 相关阅读:
    友元,静态关键字,静态方法以及对象间的关系
    体感互动游戏VR游戏AR体感游戏软件开发
    【Mysql】B+树索引的使用(七)
    Linux文件目录命令与权限(CentOS7)
    汽车以太网IOP测试新利器
    MySQL的复合查询
    我在京东的第417天:陷入了情绪的泥沼
    进程与线程
    java运行linux命令时报错
    9、定义错误页
  • 原文地址:https://blog.csdn.net/qq_70244454/article/details/128106809