Use of the open and close functions

Source: Internet
Author: User
Tags truncated

Open function

Call the Open function to open or create a file

#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);


The pathname parameter is the name of the file to be opened or created, and, like fopen, pathname can be either a relative path or

can be an absolute path. The flags parameter is used to illustrate several options for this function. "or" with one or more of the following constants

The operation forms the flags parameter. (constants are defined in the Fcntl.h header file)

One must be specified in the following three constants, and only one is allowed.

* O_rdonly Read-only Open

* O_wronly only write Open

* O_rdwr readable writable Open


The following options can be specified for 0 or more, and the required option is bitwise or up as the flags parameter. There are many options available,

Here are just a few of the other options to refer to the man Page of Open (2):

* O_append means append. If the file already has content, the data written by this open file is appended to the end of the file

Without overwriting the original content.

* O_creat If this file does not exist, create it. A third parameter, mode, is required when using this option, which indicates that the

The access rights of the file.

* O_EXCL If O_creat is specified at the same time and the file already exists, an error is returned.

* O_trunc If the file already exists and is opened in write-only or readable writeable mode, the length is truncated (trun-

Cate) is 0 bytes.

* O_nonblock for device files, open O_nonblock mode can do non-blocking I/O (nonblock i/

O)


Note that the file descriptor returned by the Open function must be the smallest unused descriptor value. A process opens 3 file descriptors by default,

That

Stdin_fileno 0

Stdout_fileno 1

Stderr_fileno 2


Note that the open function is slightly different from the fopen function of the C standard I/O library:

When a file is fopen in a writable manner, it is created automatically if the file does not exist, and the open file must be

Explicitly specify o_creat to create the file, otherwise the file does not exist and the error is returned.

When a file is fopen in W or w+, if the file already exists, it is truncated to 0 bytes, and the open file must be

You must explicitly specify O_TRUNC to truncate the file or overwrite it directly on the original data.

The third parameter, mode, specifies file permissions, which can be expressed in octal numbers, such as 0644 for-rw-r-r–, or

Use the S_IRUSR, S_IWUSR, and other macros to define the bitwise or UP, see the Man Page of Open (2). It's important to note that

The file permissions are determined by the open mode parameter and the Umask mask of the current process.

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6F/DB/wKioL1Wr0p7hy6beAAEfjSt3Arg784.jpg "title=" code. png "alt=" Wkiol1wr0p7hy6beaaefjst3arg784.jpg "/>

Operation Result:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/6F/DF/wKiom1Wr0NORUKufAAE0BmWsNSM560.jpg "title=" Run the result. png "alt=" wkiom1wr0norukufaae0bmwsnsm560.jpg "/>


The Close function:

The close function closes an open file:

#include <unistd.h>

int close (int fd);

Return value: Successfully returned 0, error returned-1 and set errno


The parameter fd is the file descriptor to be closed. It is necessary to note that when a process terminates, the kernel

The file descriptor called close has not been closed, so even if the user program does not call close, the kernel will

Automatically closes all files it opens. But for a program that is running all the ages (like a Web server), open

The file descriptor must remember to close, otherwise as more open files, will occupy a large number of file descriptors and system

Resources.

The file descriptor returned by open must be the smallest descriptor that the process has not yet used. As the program starts, it automatically hits the

Open File descriptor 0, 1, 2, so the first call to open opening file will usually return descriptor 3, and then call Open will

Returns 4. You can use this to open a new file on standard input, standard output, or standard error output to achieve a heavy

Directional functions. For example, first call close to close file descriptor 1, and then call open for a regular file,

The file descriptor 1 must be returned, when the standard output is no longer a terminal, but a regular file, and then call

printf does not print to the screen, but writes to the file.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/6F/DB/wKioL1Wr0vLwWtlJAAFNfARKRU0960.jpg "title=" Close.png "alt=" Wkiol1wr0vlwwtljaafnfarkru0960.jpg "/>

Operation Result:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/6F/DF/wKiom1Wr0SKxqfZwAAGnN8-xzyw372.jpg "title=" Close result. png "alt=" wkiom1wr0skxqfzwaagnn8-xzyw372.jpg "/>



This article is from the "Mr. Cat's Nest" blog, please be sure to keep this source http://forker.blog.51cto.com/10534711/1676213

Use of the open and close functions

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.