3.1 Introduction
File I/O functions: Open files, read files, write files
Common to five functions: open, read, write, Lseek, close.
The functions described in this chapter are: I/O (unbuffered I/O)without buffering, which means that each read and write is called by a system call in the kernel
3.2 File Descriptor
For the kernel, all open files are referenced by a file descriptor
When reading or writing a file, use the file descriptor returned by open or creat to mark the file and pass its arguments to read or write
Typically file descriptor 0 is associated with a standard input, 1 is associated with a standard output, 2 is associated with a standard error, and Stdin_fileno, Stdout_fileno, Stderr_fileno are used to improve readability
3.3 Functions Open and Openat
The file descriptor returned by the open and Openat functions must be the least unused file descriptor
Create or open a file using open or Openat
#include <fcntl.h>int open (const char *path, int oflag, .../*mode_t mode*/); int openat (int fd, const char *path, int Oflag, .../*mode_t mode*/)
The Oflag parameter can be used to
describes several options for this function, a "or" operation is made with one or more of the following constants to form the Oflag parameter
O_rdonly read-only Open
O_wronly only Write Open
O_RDWR read-write open usually 0,1,2
O_exec only perform Open
O_search only search open (app and directory)
And a lot of apue P50.
The FD parameter separates the open and Openat functions.
(1) The path parameter refers to an absolute path, and FD can be ignored Open=openat
(2) path refers to a relative path, and the FD parameter indicates the starting address of the relative path name in the file system. The FD parameter is obtained by opening the file directory where the relative path file name resides.
(3) path refers to a relative path, and the FD parameter has a special value of AT_FDCWD, in which case the path name is obtained in the current directory
The Openat function wants to solve two problems :
(1) allows a thread to open a file in a directory using a relative pathname, rather than just opening the current working directory
(2) Avoid time-of-check-to-time-of-use (Tocttou) Error: The basic idea of this error is if two file-based function calls, where the second call depends on the result of the first call then the program is fragile, because once the file changes, The structure of the second call might not be the right one.
File name and path name truncation
If Name_max is 14 and the file name is exactly 14 characters, then any function that takes the pathname as a parameter cannot know what the file's original name is. The reason is that these functions cannot tell if a function has been intercepted.
Modern file system support file name length can be 255, so for the vast majority of applications have not had this problem
3.4 Function creat
You can use creat to create a new file
#include <fcntl.h>int creat (const char *path, mode_t mode);
This function is equivalent to
Open (Path, o_eronly| O_creat| O_trunc, mode);
O_creat: Create this function when it is not present
O_trunc: If the secondary file exists and is open for write-only or read-write success, the length is truncated to 0
In earlier versions of UNIX systems, the second parameter of Open was only 0,1,2. Cannot open a file that does not already exist, so a system call creat is required to create a file
One disadvantage of creat is that it can only create files in a write-only manner. Before providing a new version of open, if you want to create a temporary file, and then read the temporary file, you must first call Creat,close and then call Open, you can now call directly
Open (Path, o_rdwr| O_creat| O_trunc, mode);
This allows you to create a file that can be read and written directly using open
3.5 function Close
You can call the close function to close the file
#include <unistd.h>int close (int fd);
When you close a file, the process also frees all record locks on that file
When a process shuts down, the kernel automatically closes all open files of the process, and many programs take advantage of this feature to not display the closed files
3.6 function Lseek
Each open file has a " current file offset " associated with it to measure the number of bytes from the beginning of the file
Usually read and write operations start at the current file offset
By default, when a file is opened, the file offset is set to zero unless the o_append option is established
You can call Lseek to set the file offset for an open file
#include <unistd.h>off_t lseek (int fd, off_t offset, int whence);
The explanation of the parameter offset is related to the value of the parameter whence, and if the execution succeeds in returning a new file offset, an error returns 1 indicating that the file cannot set an offset
(1) whence is Seek_set, set the file offset to the start of the file offset
(2) whence is Seek_cup, set the file offset to the current value plus offset
(3) whence is seek_end, the offset of the file is set to the file length plus offset, offset can be a positive negative
Some devices allow offsets to be negative , so be careful when comparing lseek return values, and do not test for less than 0, but equal to 1
Lseek only records the current file offset in the kernel, and it does not cause any I/O operations. Offset for next read and write operation
File offsets can be larger than file lengths to form holes, voids do not occupy the storage area
(not to be continued)
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
The 3rd chapter of Advanced Programming for UNIX environments: file I/O