Open () function

Source: Internet
Author: User

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

For the Open function, the third parameter is used only when a new file is created (that is, when o_creat is used) to specify the access bit (access permission bits) for the file.

Pathname is a POSIX pathname (such as/home/user/a.cpp) to open/create a file;

Flags are used to specify the open/create mode of a file, which can be composed of logical bits or logic by the following constants (defined by Fcntl.h).

O_RDONLY只读模式 O_WRONLY只写模式 O_RDWR读写模式 The above three flags are mutually exclusive, that is, cannot be used simultaneously, but can be used with the following flags to take advantage of or (|) operator combinations. O_CREAT若欲打开的文件不存在则自动建立该文件。 O_EXCL如果O_CREAT也被设置,此指令会去检查文件是否存在。文件若不存在则建立该文件,否则将导致打开文件错误。此外,若O_CREAT与O_EXCL同时设置,并且欲打开的文件为符      号连接,则会打开文件失败。 O_NOCTTY如果欲打开的文件为终端机设备时,则不会将该终端机当成进程控制终端机。 O_TRUNC若文件存在并且以可写的方式打开时,此标志位会令文件长度清为0,而原来存于该文件的资料也会消失。 O_APPEND当读写文件时会从文件尾开始移动,也就是所写入的数据会以附加的方式加入到文件后面。 O_NONBLOCK以不可阻断的方式打开文件,也就是无论有无数据读取或等待,都会立即返回进程之中。 O_NDELAY同O_NONBLOCK。 O_SYNC以同步的方式打开文件。 O_NOFOLLOW如果参数pathname所指的文件为一符号连接,则会令打开文件失败。 O_DIRECTORY如果参数pathname所指的文件并非为一目录,则会令打开文件失败。parameter ModeThis is special for Linux2.2. flag BitTo avoid some system security issues. The parameter mode has the following combinations, which will only take effect when a new file is created, and the permissions of the real file will be affected by the umask value, so the file permission should be (Mode-umaks). Example:


#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
int main (void)
{

Char a[] = {"Abc.txt"};
int fd = open (A, o_rdonly);

if (fd = =-1)
{
printf ("%s\n", Strerror (errno));//Determine if Open is successful
}
Else
{
printf ("%d\n", FD);
Close (FD);//When you are finished, remember to close the file
}

return exit_success;
}

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.