[Linux] System call IO interface

int open()

int open(const char* pathname,int flags,mode_t mode);

pathname : file path name
mode : file permission setting, 0777 (the preceding 0 cannot be omitted);-if you use O_CREAT, you must specify the file permission
flags : flag bit

Must choose one: O_RDONLY (read only), O_WRONLY (write only), O_RDWR (read and write)
optional options:

O_CREAT: create the file if it does not exist;
O_EXCL: if the file already exists, report an error and return;
O_TRUNC: clear the content when opening the file;
O_APPEND: (when opening the file, the file read and write position is cheaper to the end of the file), set the write to append In, always append the data to the end of the file.

Return value : success , return operation handle-file descriptor- non-negative integer ; failure, return -1 .

ssize_t write()

ssize_t write(int fd,const void* buf,size_t count);

Function: write data to the file
fd: the operation handle returned by open, used to clarify which file is being operated
buf: the data
to be written count: the length of the data to be written
Return value: success, returns the actual length of the data written ; Failure, return -1;

ssize_t read()

ssize_t read(int fd, void* buf,size_t count);

Function: read file content

fd: The operation handle returned by open, which is used to indicate which file is being operated.
buf: the buffer for storing the read data
count: the length of the data to be read
Return value: success, returns the actual length of the data read; failure , Return -1;

off_t lseek()

off_t lseek(int fd, off_t offset,int whence);

The operation handle returned by fd:open
offset: offset
why: relative starting position of the offset: SEEK_STE / SEEK_CUR / SEEK_END;
Return value: success, return the offset of the jumped position relative to the starting position of the file ( If you jump to the end of the file, the length of the file is returned); if it fails, it returns -1;

int close()

int close(int fd);

Close the file;
fd: operation handle returned by open

Guess you like

Origin blog.csdn.net/weixin_43962381/article/details/115303927