Most file I/O in Unix systems requires only 5 functions: Open,read,write,lseek and close
The file descriptor file descriptor is a non-negative integer, and all open files are referenced by the file descriptor to the range of changes in the file descriptor is 0-open_max,open_max is the most open file per process
1. Open function
#include <fcntl.h>
int open (const char* pathname,
int flag,
mode_t mode
); Returns the file descriptor if successful, or 1 if an error occurs
There are 3 ways to open flag: o_rdonly read-only Open
O_wronly only Write Open
O_RDWR Read, write open
These three are mutually exclusive and cannot be used at the same time, the following constants are optional:
O_append each write operation to the end of the file
O_creat If the specified file does not exist, the file is created
O_EXCL returns 1 if the file to be created already exists, and modifies the value of errno
O_trunc if the file exists and opens in write/read-only mode, the entire contents of the file are emptied
O_noctty if the path name is pointing to the end device, do not use it as a control terminal.
O_nonblock if the pathname points to a fifo/block file/character file, the open and subsequent I/O of the file are set to non-blocking (nonblocking mode)
Most implementations define O_RDONLY as 0,o_wronly defined as 1,o_rdwr defined as 2
The mode parameter is used only when creating a new file to set the initial value of the file access permission.
S_irusr,s_iwuser,s_ixusr,s_irgrp,s_iwgrp,s_ixgrp,s_iroth,s_iwoth,s_ Ixoth. where r: Read, W: Write, X: Execute, USR: the user to which the file belongs, GRP: The group to which the file belongs, OTH: other users.
2. Create function
#include <fcntl.h>
int Create (const char* Pathname,
mode_t mode
); Returns a write-only open file descriptor if successful, or 1 if an error occurs
Equivalent to open (Pathname,o_wronly | O_create | o| Trunc,mode);
Create insufficient: It opens the created file as a write-only method, and when you need to write the file first, and then read the file, you can use open instead, such as:
Open (Pathname,o_rdwr | O_create | O_trunc,mode);
3. Read function
#include <unistd.h>
ssize_t Read (int fd,//File descriptor
void *buf,//data to be written to the kernel object
size_t Size//Space
); Returns the number of bytes read if successful, or 0 if the end of the file returns 1 if there is an error.
4. Write function
#include <unistd.h>
sszie_t Write (int fd,//File descriptor
const void *BUF,//data to be written to the kernel object
size_t size//write Data sizes
); Returns the number of bytes written if successful, or 1 if an error occurs
5. Lseek function
Lseek set its offset for an open file
#include <unistd.h>
off_t lseek (int fd,//positional file description symbol
off_t offset,//positioning position
int whence//Position reference point: File start position/file end position/file current position
); Returns a new file offset if successful, or 1 if an error occurs
Parameter whence:seek_set: Offset of the file at the beginning of the file offset bytes
Seek_cur: Current position plus offset
Seek_end: File end position plus offset
6. Close function
#include <unistd.h>
int close (int fd); Returns 0 if successful, or 1 if an error occurs
Closing a file also frees all record locks that the process adds to the file, and when a process terminates, the kernel automatically closes all of its open files
Linux Learning notes one of the 10--file I/O