C Language Open () function: Open File function
Related functions: Read, Write, fcntl, close, link, stat, umask, unlink, fopen
Header files: #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h>
To define a function:
int open (const char * pathname, int flags);
int open (const char * pathname, int flags, mode_t mode);
Function Description:
Parameter pathname point to the file path string you want to open. The following are flags that the parameter flags can use:
O_rdonly open a file as read-only
O_wronly open files in write-only mode
O_RDWR Open the file in a read-write manner. The above three flags are mutually exclusive, that is, can not be used concurrently, but may be with the following flags using or (|) operator combinations.
O_creat the file is automatically created if the file you want to open does not exist.
O_excl If the o_creat is also set, this instruction checks to see if the file exists. If the file does not exist, the file is created, or it will cause a file error to open. In addition, if the o_creat is set with O_EXCL and the file you want to open is a symbolic connection, then the file will fail to open.
O_noctty If the file you want to open is a terminal device, it will not be treated as a Process control terminal.
O_trunc If a file exists and is opened in a writable manner, this flag rcas the length of the file to 0 and the data originally stored in the file disappears.
O_append is moved from the end of the file when reading and writing the file, which means that the data being written is added to the file in an additional way.
The O_nonblock opens the file in an unstoppable manner, which means that it is immediately returned to the process, regardless of whether there is any data read or waiting.
O_ndelay with O_nonblock.
O_sync Open the file in a synchronized fashion.
O_nofollow if the parameter pathname the file you refer to is a symbolic connection, the open file will fail.
O_directory If the parameter pathname refers to a file that is not a directory, the open file will fail. Note: This is Linux2. 2 Special flag to avoid some system security problems.
parameter mode has the following combinations that take effect only when a new file is created, and the permissions on the real file are affected by the umask value, so the file permissions should be (Mode-umaks).
s_irwxu00700 permissions that represent the file owner with readable, writable, and executable permissions.
S_irusr or S_iread, 00400 permissions, on behalf of the file owner with readable permissions.
S_iwusr or S_iwrite, 00200 permissions, on behalf of the file owner with writable permissions.
S_ixusr or S_iexec, 00100 permissions, on behalf of the file owner with executable permissions.
S_irwxg 00070 permissions to represent the file user group with readable, writable, and executable permissions.
S_IRGRP 00040 permissions, which represent that the file user group has readable permissions.
S_IWGRP 00020 permissions to represent the file user group with writable permissions.
S_IXGRP 00010 permissions to represent the file user group with executable permissions.
S_irwxo 00007 permissions to represent other users with readable, writable, and executable permissions.
S_iroth 00004 permissions on behalf of other users with readable permissions
S_iwoth 00002 permissions on behalf of other users with writable permissions.
S_ixoth 00001 permissions, on behalf of other users have executable permissions.
Return value: If all the permissions to be checked are passed the check returns a value of 0, indicating success, as long as a permission is prohibited to return-1.
Error code:
The eexist parameter pathname refers to a file that already exists, but uses the o_creat and O_EXCL flags.
The eaccess parameter pathname refers to a file that does not meet the permissions required for the test.
Erofs the file you want to test write permission exists in a read-only file system.
The Efault parameter pathname pointer exceeds the accessible memory space.
Einval parameter mode is incorrect.
Enametoolong parameter pathname too long.
Enotdir parameter pathname is not a directory.
Enomem core memory is low.
Eloop parameter pathname has too many symbolic connection problems.
Eio I/O access error.
Additional instructions: use Access () as a user authentication judgment to be especially cautious, such as open () empty files after access () can cause problems with system security.
Example
<strong> #include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
# Include <fcntl.h>
main ()
{
<span style= "White-space:pre" > </span>int fd, size;
<span style= "White-space:pre" > </span>char s[] = "Hello world!\n", buffer[80];
</strong>
<strong><span style= "White-space:pre" > </span>fd = open ("/tmp/temp", o_wronly| O_creat);
<span style= "White-space:pre" > </span></strong>
<strong><span style= "White-space:pre" > </span>write (FD, S, sizeof (s));
<span style= "White-space:pre" > </span>close (FD);
</strong>
<strong><span style= "White-space:pre" > </span>fd = open ("/tmp/temp", o_rdonly);
<span style= "White-space:pre" > </span>size = Read (fd, buffer, sizeof (buffer));
<span style= "White-space:pre" > </span></strong>
<strong><span style= "White-space:pre" > </span>close (FD);
<span style= "White-space:pre" > </span>printf ("%s", buffer);
</strong>
Perform
Hello world!