Several sets of interfaces for file operations

Source: Internet
Author: User

Basic interface:

File Read and write:

#include <sys/types.h>

#include <sys/stat.h>

#include <fcntl.h>

int open (const char* pathname,int flags)

int open (const char* pathname,int flags,mode_t mode);

int creat (const char *pathname, mode_t mode)

Parameter description:

Pathname: File name to open

Flags: flag bits that specify how the file is opened and how it behaves when it is opened.

Mode: A bit of permission flags for new files.

return value:

-1: The operation failed and set the value of the variable errno

Not to-1: File descriptor,


The flags parameter can be evaluated as:

1, o_rdonly: Open the file in a read-only way

2, O_wronly: Open the file in a write-only way

3, O_rdwr: Read and write the way to open the file

The above three values are mutually exclusive and cannot be used at the same time, but they can be combined with the following values or methods:

1, O_creat: If the file that is opened does not exist, will automatically wear this file

2. O_EXCL: If the O_CREAT flag is already in use, the Open function fails to return when the file specified by the pathname parameter already exists. If pathname gives a symbolic link, the call to the open function returns a failure, regardless of whether the file it points to exists.

3. o_nocity: If the file being opened is a terminal device file, it will not become the control terminal of this process.

4, O_trunc: If the opened file exists and is open in a writable manner, then empty the original contents of the file

5, O_append: The newly written content will be added to the original content of the file behind,

6, O_nonblock: The opened file will be in a non-blocking way to operate

O_nonblock the same. 7, O_ndelay: With

8, O_sync: The opened file will be synchronized I/O to operate, that is, any write operation will be synchronized to the hardware device. When the synchronization completes, calls to the write function are returned.

9, O_nofollow: If pathname is a symbolic link, the call to the open function will return a failure.

10, O_directory: If pathname is not a directory, then the call to the open function will return a failure.

The thing to note is that the open function has two prototypes, one of which is out of mode, which specifies the access to the new file created, and if the O_CREAT flag is used to create the file, then the Mode argument is generally given. Some of her common values, shown in table 1.0, can be used in a bitwise OR combination. The user who owns the new file and the combination of the suit

S_irwxu 00700 File-owned users have read-write and Execute permissions
S_IRUSR (S_iread) 00400 The user who owns the file has Read permission
S_IMUSR (S_iwrite) 00200 The user who owns the file has write permissions
S_IXUSR (s_iexec) 00100 The user who owns the file has execute permissions
S_irwxg 00070 Users in the group have read and write and Execute permissions
S_irgrp 00040 Read permissions for users in a group
S_iwgrp 00020 User in group has write permission
S_ixgrp 00010 Users in a group have Execute permissions
S_irwxo 00007 Other users have read-write and Execute permissions
S_iroth 00004 Read permissions for users in a group
S_iwoth 00002 User in group has write permission
S_ixoth 00001 Users in a group have Execute permissions

Because the O_wronly,o_creat,o_trunc three flags are often combined when the open function is invoked, it is implemented by a specialized function creat (), as follows:

creat (pathname, mode)

Actual equivalent to: open (pathnames,o_wronly| O_creat| O_trunc, Mode)

Two different processes can open the same file at the same time, but their file descriptors are generally different, and they can read and write to the file at the same time, but the subsequent process will overwrite what was written in the previous process.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.