|
Close (close the file) |
Related functions |
Open, fcntl, shutdown, unlink, fclose
|
Header file |
# Include <unistd. h>
|
Define functions |
Int close (int fd );
|
Function Description |
If the file is no longer needed after use, close () can be used to close the file. Second close () will write data back to the disk and release the resources occupied by the file. The FD parameter is the description of the file returned by open () or creat.
|
Return Value |
If the file is closed successfully, 0 is returned, and-1 is returned when an error occurs.
|
Error Code |
The description of an invalid file with the ebadf parameter FD or the file is disabled.
|
Additional instructions |
Although the system automatically closes opened files when the process ends, we recommend that you close the files and check the returned values.
|
Example |
Refer to open ()
|
|
|
Creat (create a file) |
Related functions |
Read, write, fcntl, close, Link, stat, umask, unlink, fopen
|
Header file |
# Include <sys/types. h> # Include <sys/STAT. h> # Include <fcntl. h>
|
Define functions |
Int creat (const char * pathname, mode_tmode );
|
Function Description |
The pathname parameter points to the file path string to be created. Creat () is equivalent to calling open () using the following call Methods () Open (const char * pathname, (o_creat | o_wronly | o_trunc ));
|
Error Code |
For more information about the parameter mode, see open () function.
|
Return Value |
Creat () returns a new file description. If an error occurs,-1 is returned, and the error code is set to errno. The file specified by the eexist parameter pathname already exists. The file specified by the eaccess parameter pathname does not meet the required permissions. The file to which erofs wants to open the write permission exists in the read-only file system. The pathname pointer of the efault parameter exceeds the accessible memory space. The Mode Val parameter mode is incorrect. The pathname parameter of enametoolong is too long. The pathname parameter of enotdir is a directory. Insufficient enomem core memory The pathname parameter of eloop has too many symbolic connections. Emfile has reached the maximum number of files that can be opened simultaneously by the process. Enfile has reached the maximum number of files that can be opened simultaneously by the system.
|
Additional instructions |
Creat () cannot create special device files. Use mknod () if necessary ().
|
Example |
See open ().
|
|
|
DUP (copy file description) |
Related functions |
Open, close, fcntl, dup2
|
Header file |
# Include <unistd. h>
|
Define functions |
Int DUP (INT oldfd );
|
Function Description |
DUP () is used to copy the file description referred to by the oldfd parameter and return it. The new file description and parameter oldfd refer to the same file, sharing all the locking, read/write locations, and various permissions or flag. For example, when lseek () is used to describe a file, the read/write location of the descriptive word of another file also changes. However, file descriptions do not share the close-on-exec flag.
|
Return Value |
When the copy is successful, the minimum and unused file description words are returned. If an error occurs,-1 is returned, and errno stores the error code. The error code ebadf parameter FD is a non-valid file description word, or the file is disabled.
|
|
|
Dup2 (copy file description) |
Related functions |
Open, close, fcntl, DUP
|
Header file |
# Include <unistd. h>
|
Define functions |
Int dup2 (INT odlfd, int newfd );
|
Function Description |
Dup2 () is used to copy the file description referred to by the oldfd parameter and copy it to the next part of the newfd parameter. If newfd is an opened file description, the file referred to by newfd is closed first. The file description word copied by dup2 () shares various file states with the original file description word. For details, see DUP ().
|
Return Value |
When the copy is successful, the minimum and unused file description words are returned. If an error occurs,-1 is returned, and errno stores the error code.
|
Additional instructions |
Dup2 () is equivalent to calling fcntl (oldfd, f_dupfd, newfd). For details, see fcntl ().
|
Error Code |
The ebadf parameter FD is a non-valid file description word, or the file is disabled.
|
|
|
Fcntl (File description operations) |
Related functions |
Open, flock
|
Header file |
# Include <unistd. h> # Include <fcntl. h>
|
Define functions |
Int fcntl (int fd, int cmd ); Int fcntl (int fd, int cmd, long Arg ); Int fcntl (int fd, int cmd, struct flock * Lock );
|
Function Description |
Fcntl () is used to operate on some features of file descriptors. The FD parameter indicates the description of the file to be set, and the CMD parameter indicates the command to be operated. There are several situations: F_dupfd is used to find the smallest and unused file description words greater than or equal to the ARG parameter, and copy the file description words of the parameter FD. If the execution is successful, the new copied file description is returned. See dup2 (). F_getfd gets the close-on-exec flag. If the fd_cloexec bit of the flag is 0, the file will not be closed when the exec () function is called. F_setfd: Set the close-on-exec flag. The flag is determined by the fd_cloexec bit of the ARG parameter. F_getfl gets the flag of the file descriptive word status, which is the flags parameter of open. F_setfl sets the State Flag of the description word in the file. The ARG parameter is the new flag, but only changes to the o_append, o_nonblock, and o_async bits are allowed. Changes to other bits are not affected. F_getlk: get the file lock status. F_setlk sets the file lock status. The l_type value of the flcok structure must be f_rdlck, f_wrlck, or f_unlck. If a lock cannot be created,-1 is returned. The error code is eacces or eagain. F_setlkw f_setlk works the same, but the lock cannot be established. This call will wait until the lock action is successful. If a signal is interrupted while waiting for the lock,-1 is returned immediately and the error code is eintr. The parameter lock pointer is a flock structure pointer and is defined as follows: Struct flcok { Short int l_type;/* locked status */ Short int l_whence;/* determines the Rochelle start position */ Off_t l_start;/* start position of the locked area */ Off_t l_len;/* size of the locked area */ Pid_t l_pid;/* Lock action process */ }; Rochelle type has three statuses: F_rdlck creates a lock for reading F_wrlck creates a lock for writing F_unlck Delete the previously created lock Rochelle whence also has three methods: Seek_set starts with a file and is the starting position of the lock. Seek_cur uses the current file read/write location as the starting position of the lock The start position of seek_end lock is the end of the file.
|
Return Value |
If the request succeeds, 0 is returned. If an error exists,-1 is returned. The error cause is stored in errno.
|
|
|
Flock (lock file or unlock) |
Related functions |
Open, fcntl
|
Header file |
# Include <sys/file. h>
|
Define functions |
Int flock (int fd, int operation );
|
Function Description |
Flock () performs various locking or unlocking actions on the file referred to by the parameter FD according to the method specified by the parameter operation. This function can only lock the entire file and cannot lock a certain area of the file.
|
Parameters |
Operation has the following four conditions: Lock_sh creates a shared lock. Multiple processes can share and lock the same file at the same time. Lock_ex creates a mutex lock. One file has only one mutex lock. Lock_un unlocks the file. Lock_nb cannot be used to establish a lock. This operation is not blocked and the process is returned immediately. Usually it is combined with lock_sh or lock_ex or (|. Shared locks and mutex locks cannot be created for a single file. When DUP () or fork () is used, the file description does not inherit the lock.
|
Return Value |
If the return value is 0, an error is returned. If an error occurs, the return value is-1. The error code is stored in errno.
|
|
|
Fsync (write buffer data back to disk) |
Related functions |
Sync
|
Header file |
# Include <unistd. h>
|
Define functions |
Int fsync (int fd );
|
Function Description |
Fsync () is responsible for writing the file data referred to by the FD parameter back to the disk by the system buffer to ensure data synchronization.
|
Return Value |
0 is returned for success,-1 is returned for failure, and errno is the error code.
|
|
|
Lseek (read/write location of mobile files) |
Related functions |
Dup, open, fseek
|
Header file |
# Include <sys/types. h> # Include <unistd. h>
|
Define functions |
Off_t lseek (INT Fildes, off_t offset, int whence );
|
Function Description |
Each opened file has a read/write location. When a file is opened, its read/write Location usually points to the beginning of the file. If the file is opened in an additional way (such as o_append ), the read/write location points to the end of the file. When read () or write (), the read/write location increases accordingly, and lseek () is used to control the read/write location of the file. The Fildes parameter is the description of an opened file, and the offset parameter is the number of read/write locations to be moved Based on the whence parameter.
|
Parameters |
Whence is one of the following: The seek_set parameter offset is the new read/write location. Seek_cur increases the offset displacement at the current read/write position. Seek_end points the read/write position to the end of the file and then increases the offset displacement. When the whence value is seek_cur or seek_end, The offet parameter allows negative values. Here are some special instructions: 1) to move the read/write position to the beginning of the file: lseek (INT Fildes, 0, seek_set ); 2) When you want to move the read/write position to the end of the file: lseek (INT Fildes, 0, seek_end ); 3) to obtain the current file location: lseek (INT Fildes, 0, seek_cur );
|
Return Value |
When the call is successful, the current read/write location is returned, that is, the number of bytes from the beginning of the file. If an error occurs,-1 is returned, and errno stores the error code.
|
Additional instructions |
Linux does not allow lseek () to act on the tty device. This action causes lseek () to return espipe.
|
Example |
Refer to this function description
|
|
|
Mkstemp (create a unique temporary file) |
Related functions |
Mktemp
|
Header file |
# Include <stdlib. h>
|
Define functions |
Int mkstemp (char * template );
|
Function Description |
Mkstemp () is used to create a unique temporary file. The last six characters in the file name string referred to by the template parameter must be xxxxxx. Mkstemp () can open the file in read/write mode and with 0600 permissions. If the file does not exist, the file will be created. After the file is opened, its description is returned. After the file is successfully opened, the system returns the description word of the file that can be read and written. If the file fails to be opened, null is returned and the error code is stored in errno.
|
Error Code |
The last six characters of the template string of the einval parameter are not xxxxxx. Eexist cannot create temporary files.
|
Additional instructions |
The file name string referred to by the template parameter must be declared as an array, for example: Char template [] = "template-xxxxxx "; Do not use the following expressions Char * template = "template-xxxxxx ";
|
Example |
# Include <stdlib. h> Main () { Int FD; Char template [] = "template-xxxxxx "; FD = mkstemp (Template ); Printf ("template = % s/n", template ); Close (FD ); }
|
Run |
Template = template-lgzcbo
|
|
|
Open (open a file) |
Related functions |
Read, write, fcntl, close, Link, stat, umask, unlink, fopen
|
Header file |
# Include <sys/types. h> # Include <sys/STAT. h> # Include <fcntl. h>
|
Define functions |
Int open (const char * pathname, int flags ); Int open (const char * pathname, int flags, mode_t mode );
|
Function Description |
The pathname parameter points to the file path string to be opened. The following are flags that can be used by the flags parameter: O_rdonly open a file in read-only mode O_wronly open the file in write-only mode O_rdwr can open files in read/write mode. The above three flags are mutually exclusive, that is, they cannot be used at the same time, but can be combined with the following flags using the OR (|) operator. O_creat: if the file to be opened does not exist, the file is automatically created. O_excl if o_creat is also set, this command checks whether the file exists. If the file does not exist, the file will be created; otherwise, the file will be opened incorrectly. In addition, if both o_creat and o_excl are set and the file to be opened is a symbolic connection, opening the file will fail. If the file to be opened is a terminal device, o_noctty does not regard the terminal as a process control terminal. If the o_trunc file exists and is opened in writable mode, this flag will clear the file length to 0, and the information originally stored in the file will also disappear. O_append when reading and writing a file, it will start to move from the end of the file, that is, the written data will be appended to the end of the file. O_nonblock open the file in an unblocking way, that is, whether there is data read or waiting, it will immediately return to the process. O_ndelay is the same as o_nonblock. O_sync opens the file in synchronous mode. O_nofollow if the file indicated by pathname is a symbolic connection, opening the file will fail. O_directory if the file indicated by the pathname parameter is not a directory, opening the file will fail. This is a flag unique after linux2.2 to avoid some system security problems. The mode parameter has the following combinations. This parameter takes effect only when a new file is created. In addition, the permissions for creating a file are affected by the umask value, therefore, the file permission should be (Mode-umaks ). S_irwxu00700 permission indicates that the object owner has the readable, writable, and executable permissions. S_irusr, s_iread, and 00400 permissions indicate that the file owner has the readable permission. S_iwusr or s_iwrite, 00200 permission indicates that the file owner has the write permission. S_ixusr or s_iexec, 00100 permission indicates that the file owner has executable permission. S_irwxg 00070 permission indicates that the file user group has the readable, writable, and executable permissions. S_irgrp 00040 permission indicates that the file user group has the readable permission. S_iwgrp 00020 permission indicates that the file user group has the write permission. S_ixgrp 00010 permission indicates that the file user group has executable permissions. S_irwxo 00007 indicates that other users have the readable, writable, and executable permissions. S_iroth 00004 permission, which indicates that other users have the readable permission S_iwoth 00002 permission indicates that other users have the write permission. S_ixoth 00001 permission indicates that other users have executable permissions.
|
Return Value |
If all the permissions to be verified have passed the check, a value of 0 is returned, indicating that the operation is successful. If one permission is disabled, the system returns-1.
|
Error Code |
The file indicated by the eexist parameter pathname already exists, but the o_creat and o_excl flag are used. The file indicated by the eaccess parameter pathname does not meet the required permissions. The file to be tested by erofs is stored in the read-only file system. The pathname pointer of the efault parameter exceeds the accessible memory space. The Mode Val parameter mode is incorrect. The pathname parameter of enametoolong is too long. The pathname parameter of enotdir is not a directory. The enomem core memory is insufficient. The pathname parameter of eloop has too many symbolic connections. Eio I/O access error.
|
Additional instructions |
Be especially careful when using access () for user authentication. For example, making an open () empty file after access () may cause system security problems.
|
Example |
# Include <unistd. h> # Include <sys/types. h> # Include <sys/STAT. h> # Include <fcntl. h> Main () { Int FD, size; Char s [] = "Linux programmer! /N ", buffer [80]; FD = open ("/tmp/Temp", o_wronly | o_creat ); Write (FD, S, sizeof (s )); Close (FD ); FD = open ("/tmp/Temp", o_rdonly ); Size = read (FD, buffer, sizeof (buffer )); Close (FD ); Printf ("% s", buffer ); }
|
Run |
Linux programmer!
|
|
|
Read (read data from opened files) |
Related functions |
Readdir, write, fcntl, close, lseek, readlink, fread
|
Header file |
# Include <unistd. h>
|
Define functions |
Ssize_t read (int fd, void * Buf, size_t count );
|
Function Description |
Read () transfers the file referred to by the FD parameter to the memory indicated by the Buf pointer in count bytes. If the Count parameter is 0, read () does not work and 0 is returned. The returned value is the number of bytes actually read. If 0 is returned, it indicates that the data has reached the end of the file or cannot be read. In addition, the read/write location of the file will move with the read bytes.
|
Additional instructions |
If the read () operation succeeds, the actual number of bytes read will be returned. It is better to compare the returned value with the Count parameter. If the returned number of bytes is less than the number of bytes required to be read, it is possible to read the end of the file, read from the pipe or terminal, or read () is interrupted by the signal. If an error occurs,-1 is returned. The error code is stored in errno, and the file read/write location is unpredictable.
|
Error Code |
The call to eintr is interrupted by the signal. Eagain: When I/O cannot be blocked (o_nonblock), this value is returned if no data can be read. The ebadf parameter FD is a non-valid file description word, or the file is closed.
|
Example |
See open ().
|
|
|
Sync (write buffer data back to disk) |
Related functions |
Fsync
|
Header file |
# Include <unistd. h>
|
Define functions |
Int sync (void)
|
Function Description |
Sync () writes the System Buffer data back to the disk to ensure data synchronization.
|
Return Value |
Returns 0.
|
|
|
Write (write data into an opened file) |
Related functions |
Open, read, fcntl, close, lseek, sync, fsync, fwrite
|
Header file |
# Include <unistd. h>
|
Define functions |
Ssize_t write (int fd, const void * Buf, size_t count );
|
Function Description |
Write () will write the memory referred to by the parameter Buf into the file referred to by the parameter FD in count bytes. Of course, the file read/write location will also move.
|
Return Value |
If write () succeeds, the actual number of bytes written will be returned. If an error occurs,-1 is returned, and the error code is stored in errno.
|
Error Code |
The call to eintr is interrupted by the signal. Eagain: When I/O cannot be blocked (o_nonblock), this value is returned if no data can be read. The EADF parameter FD is a non-valid file description word, or the file is closed.
|
Example |
See open ().
|
|