Linux/C + +: File operations--open, create, close
File descriptor
Kernel (kernel) to access the file using the file descriptor (descriptor). The file descriptor is a non-negative integer. When you open an existing file or create a new file, the kernel returns a file descriptor. Read-write files also need to use file descriptors to specify which files to read and write. The system kernel maintains a list of file descriptors for each process. as follows:
The kernel Assignment file descriptor rule is that it is always allocated unused and is minimal. The first three file descriptors: 0, 1, 2, are already occupied by default. Of course, you can close it first. such as closing 1, redistribution, can lead to redirection.
Function prototypes
#include <fcntl.h>
int open (const char *path, int flag, .../*mode_t mode*/);
Function: Open File
Return value: Successful, returns the file descriptor; 1 If an error occurs
Parameters:
Path file paths
The open mode of the flag file
Common mode: o_rdonly read-only O_wronly Write only O_rdwr read/write o_exec perform O_search search (applies to directory) (must, five select one)
Optional: o_append append o_creat file does not exist, then create.
... File permissions
Function prototypes
#include <fcntl.h>
int Create (const char *path, mode_t mode);
Features: Creating files
Return value: Successful, returns the file descriptor; 1 If an error occurs
Parameters:
Path file paths
Mode file permissions
Function prototypes
#include <unistd.h>
int close (int fd);
Function: Close open files, release resources
Parameter: FD is pending file descriptor
Return: Success, return 0; error, return-1
It is necessary to note that when the process is finished, the kernel shuts down the files opened by the process.
Directory of all content
Linux/C + +: File operations--open, read, write