Creat Open System call

Source: Internet
Author: User

Creat:

To create a file, it is obvious that the parameter only needs (path, permission mode).

1. What if the file exists? Cover it straight out! No matter what you have inside.

#include <sys/types.h> #include <fcntl.h> #include <sys/stat.h> #include <iostream>using namespace Std;int Main () {        int fd;        umask (0000);        Fd=creat ("Grb.txt", 0777);        if (fd==0)                cout<< "Create failed!\n";        else                cout<< "Create successful!\n";}

  

The creation was successful, but there was a problem.

The permissions given to octal 777 are the maximum permissions, but the view permissions are not.

The value of mode set here is not the final permission of the file, the final permission of the file is obtained according to mode & ~umask, and the default umask is 0022, so if you want to set file permissions according to the value of mode, you need to set the value of umask to 0000 before creating the file. Add a sentence umask (0000), you can. Open:Let's start by analyzing what the open needs to provide.     1, to open a file, it is obvious to give the path, and open the way flag (read-only, write-only, readable writable). The opening of the file and permissions are different, such as the user of the file has all permissions, but it can also read-only open the file, I do not have permission to use. 2. What if the open file does not exist? File not exit should be needed! Error. You should also be prompted whether to create such a new file, which requires a third parameter, mode, to represent the permissions of the newly created file.summarize the usage of creat and open in Linux: about creating a new file:
Use the CREATE function: Create a file that is write-only if used with the creat (path, mode) function. Because the return value of creat is also a FD. So you can use this FD immediately, but only write! 1, the file does not exist when: Create a new file. Write only. 2, the file exists, the direct zeroing, regardless of the previous file whether there is content. Equivalent to open (path,o_wronly| Creat| O_ trunc,0777) use open to do whatever you want:
    1. File does not exist, can create read-only, write-only, or readable writable files
    2. When a file exists, it can be cleared 0, or appended.
    3. File exits! can be error when files exist.
Therefore, it can be created without creating at all, and then using open to create it. about opening files:Opening a file can only be done with the open function.       Returns an FD that has a read-only, write-only, readable writable three basic way to open a file.         Other: Append method, O_append if not set, will write from scratch. Zero, each time you write, first clear 0 (this only in Write-only modeEffective). Several instances 1, create a file, and give maximum permissions, if the file already exists, then an error.
#include <stdio.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h>int Main () {        umask (0000);        int Fd=open ("Guo0.txt", o_excl| o_creat,0777);//Create a new file and give the maximum permission, if present, error.        if (fd==-1)        {                printf ("creat failed!\n");        }        else        {                printf ("creat successful!\n");        }        Close (FD);        return 0;}

Creat Open System call

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.