File descriptor
For the kernel, all open files are referenced by a file descriptor. The file descriptor is a nonnegative integer, and each process defaults to three open file descriptors associated with it, namely standard input 0, standard output 1, and standard error 2. The POSIX specification defines three symbolic constants, Stdin_fileno,stdout_fileno and Stderr_fileno, in the header file unistd.h.
The number of files that can be opened at the same time by any one process is limited, which is usually defined by the constant Open_max in the Limits.h header file, and its value varies with the system, but the POSIX specification requires it to be at least 16.
Open function
#include <fcntl.h>int open (constcharint/** / );
The Open function opens or creates a file that returns 1 if the file descriptor is successfully returned.
Pathname is the name of the file to open or create.
The Oflag parameter is the result of one or more of the following constants that perform a bitwise OR Operation kill
- O_rdonly read-only Open
- O_wronly only Write Open
- O_RDWR Read/write Open
The above three constants must specify one and only one, and some of the following constants are optional:
- O_append Append writes to the end of the file
- O_creat If the file does not exist, create it. With this option, a third parameter, mode, is required to specify the access permission bit for the new file
- O_EXCL if O_creat is specified and the file already exists, an error occurs
- O_trunc If this file exists and is successfully opened for write-only or read-write mode, the length is truncated to 0
- O_noctty If pathname refers to an end device, it is not assigned as the control terminal for this process
- O_nonblock If pathname refers to a FIFO file, block device file, or character device file, this option sets the file's open operation and subsequent I/O operations to non-blocking mode
The following three flags are also optional. They are part of the synchronous input and output options in single UNIX specification (and POSIX.1):
- O_dsync makes each write wait for the physical I/O operation to complete, but does not wait for the file property to be updated if the write operation does not affect reading the data just written
- O_rsync each read operation that takes a file descriptor as a parameter waits until any unfinished writes to the same part of the file are completed
- O_sync makes each write wait for the physical I/O operation to complete, including the I/O required for file property updates caused by the write operation
The mode parameter is used only when the Oflag parameter specifies the O_creat option and is used to specify the access permission bits for the new file, which are defined in the header file Sys/stat.h:
- S_IRUSR Read permission, file owner
- S_IWUSR Write permission, file owner
- S_IXUSR Execute permissions, file owner
- S_IRGRP Read permission, group to which the file belongs
- S_IWGRP Write permissions, groups to which the file belongs
- S_IXGRP Execute permissions, group to which the file belongs
- S_iroth Read permissions, other users
- S_iwoth Write permissions, other users
- S_iroth Execute permissions, other users
There are several factors that can affect the access rights of a file. First, the specified access rights are used only when the file is created. Second, the user mask will affect the access rights of the created file, that is, the open given the mode value and the user mask of the inverse value of the result of the and operation, is the real access to the file. The file descriptor returned by open must be the smallest descriptor value that is not used.
creat function
#include <fcntl.h>int creat (constchar *pathname, mode_t mode);
You can also call the creat function to create a new file that is equivalent to: open (pathname, o_wronly | O_creat | O_trunc, mode); The function execution successfully returns the open file descriptor, otherwise returns-1.
One disadvantage of creat is that it opens the created file in a write-only manner.
Close function
#include <unistd.h>int close (int filedes);
The close function closes an open file, the execution returns 0 successfully, otherwise returns-1.
Closing a file also frees all locks that the process adds to the file. When a process terminates, the kernel automatically closes all of its open files.
Lseek function
#include <unistd.h>off_t lseek (intint whence);
Each open file has a current file offset associated with it. It is usually a nonnegative integer that measures the number of bytes computed at the beginning of the text. Typically, read and write operations start at the current file offset and increase the number of bytes read and written by the offset. By default in the system, when a file is opened, the offset is set to 0 unless the O_append option is specified.
You can call the Lseek display to set its offset for an open file. The interpretation of the parameter offset is related to the value of the parameter whence:
- If whence is seek_set, then the offset of the file is set to offset bytes from the beginning of the file.
- If whence is seek_cur, the offset of the file is set to the current value plus offset,offset can be positive or negative.
- If whence is seek_end, the file's offset is set to the file length plus offset,offset can be positive or negative.
If Lseek succeeds, returns the new file offset, otherwise returns-1.
Typically, a file's current offset should be a non-negative integer, but some devices may also allow a negative offset. For normal files, however, their offsets must be non-negative. Because the offset may be negative, you should be careful when comparing the return value of Lseek, and do not test whether it is less than 0 and whether it is equal to-1.
Lseek only records the current file offset in the kernel, and it does not cause any I/O operations. The offset is then used for the next read or write operation.
The file offset can be greater than the current length of the file, in which case the next write to the file will be extended and a hole in the file, all of the bytes that are in the file but not written are read as 0.
Read function
#include <unistd.h>ssize_t read (intvoid *buf, size_t nbytes);
The read function is called to read the data from the open file, and if the execution succeeds, the number of bytes read is returned, and if the end of the file returns 0, an error returns-1.
Write function
#include <unistd.h>ssize_t write (intconstvoid *buf, size_t nbytes);
Call the Write function to write data to the open file. The number of bytes written successfully returned by execution, and 1 if an error occurred.
The return value is usually the same as the value of the parameter nbytes, otherwise it means an error.
The following is an example program that demonstrates the use of the underlying I/O functions.
1#include <unistd.h>2#include <sys/stat.h>3#include <fcntl.h>4#include <stdlib.h>5 6 intMainvoid)7 {8 Charblock[4096];9 int inch, out;Ten intnread; One A inch= Open ("file.in", o_rdonly); - out= Open ("File.out", o_wronly| O_creat, s_irusr| s_iwusr|s_irgrp); - while(Nread = Read (inch, block,4096)) >0) { theWrite out, block, nread); - } - -Exit0); +}
File I/O