Linux I/O operation method: system call
The system call is implemented by the kernel, executed in the kernel, and called by the program (user mode). It is distinguished from the call and realization of library functions. The call, realization and execution of library functions are all in the user mode
The header file called by the file operating system: <unistd.h>
1. Open the file
int open(const char *pathname, int flag, /*int mode*/);
pathname: file path + file name
flag: open flag
-
O_RDONLY read only
-
O_WRONLY write only
-
O_RDWR read and write
One of the above three variables must be specified and only one can be specified. The following constants are optional:
-
Added O_APPEND
-
O_CREAT Open the file, create it if it does not exist
-
O_EXCL If O_CREAT is also specified, and the file already exists, an error will occur. Use this to test whether a file exists
-
O_TRUNC If this file exists, and it is opened for writing or reading and writing successfully, its length is truncated to 0
-
O_NOCTTY If pathname refers to a terminal device, the device is not assigned as the controlling terminal of this process
-
O_NONBLOCK If pathname refers to a FIFO, a block special file, or a character special file, this option is set to non-blocking mode for the current open operation of the file and subsequent I/O operations
mode: This parameter is only required when flag is O_CREAT, which specifies the access permission of the created file, returns a file descriptor (integer value greater than or equal to 0) on success, and returns -1 on failure
Return value: Return a file descriptor (integer value greater than or equal to 0) on success, -1 on failure
2. Read the content of the file
int read(int fd, void *buf, int size);
fd: The file descriptor of the file to be read (return value opened with open)
buf: user buffer, used to store data read from the file
size: The size of the user buffer, which specifies the maximum data length that can be read by a read, in bytes
Return value: Return the length of the data actually read successfully, return 0 when reading the end of the file, return -1 when reading fails
3. Write content to the file
int write(int fd, void *buf, int length);
fd: the file descriptor of the file to be written
buf: the first address of the data you want to write into the file
length: the length of the written data
Return value: return the length of the actual data written successfully, return 0 without writing anything, return -1 write failure
4. Operate the read and write cursor (offset)
int lseek(int fd, int count, int flag);
fd: file descriptor
count: the size of the move
flag: relative position
- SEEK_SET Set the offset of the file to count bytes from the beginning of the file, and set it to the file header: lseek(fd, 0, SEEK_SET);
- SEEK_CUR sets the file offset to the current offset plus count bytes
- SEEK_END sets the file offset to the end of the file plus count bytes
Return value: return 0 on success, -1 on failure
5. Close open files
int close(int fd);
fd: the file descriptor of the file to be closed
Return value: return 0 on success, -1 on failure
Closing a file will eliminate the current cursor, and the cursor will be at the head of the file next time you open it
When a process terminates, the kernel automatically closes all its open files. Many programs take advantage of this feature and use close to close open files without displaying it.
6. Example: Use the above system call to realize file copy
void copyfile(const char *source, const char *dest)
{
int fdr = open(source,O_RDONLY);
assert(fdr != -1);
int fdw = open(dest, O_WRONLY | O_CREAT | O_TRUNC, 0664);//0664数字法设定文件权限
assert(fdw != -1);
while(1)
{
char buff[128] = {
0 };
int n = read(fdr, buff, 127);
if(n <= 0)
{
break;
}
write(fdw, buff, n);
}
close(fdr);
close(fdw);
}