The difference between the creat function and the Open function

Source: Internet
Author: User

As the name implies, Creat creates a new file, open opens the file. However, open can also play a role in creating a new file.

#include <fcntl.h>int creat (const char *pathname, mode_t mode);//returns:file descriptor opened for write-only if O K, 1 On Error

When the creat file already exists, creat truncates the file's original file, and the modified time access time of the file is also modified.

creat equivalent to: open (pathname, o_wronly | O_creat | O_trunc, mode);


Open

A file is opened or created by calling the open function.

#include <fcntl.h>int open (const char *pathname, int oflag, .../*mode_t mode */);//returns:file Descriptor if O K, 1 On Error

The third parameter is only useful if you are creating a file.

One deficiency with creat is, the file is opened only for writing. Before the new version of Open is provided, if we were creating a temporary file, we wanted to write and then read ba CK, we had to call creat, close, and then open. A better-to-use of the open function, as in
Open (Pathname, O_RDWR | O_creat | O_trunc, mode);

From Apue

In general, the open feature is more powerful and includes the functionality of the creat. It is recommended to use open.

The difference between the creat function and the Open function

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.