int open(const char *pathname, int flags, …);
最后的可变参数可以是0个或1个,由flags 参数中的标志位决定,见下面的详细说明。
pathname 参数是要打开或创建的文件名,和fopen 一样,pathname 既可以是相对路径也可以是绝吵清
对路径。flags 参数有一系列常数值可供选择,可以同时选择多个常数用按位或运算符连接起
来,所以这些常数的宏定义都以O_开头,表示or。
必选项:以下三个常数中必须指定一个,且仅允许指定一个。
O_RDON 只读打开
O_WRON 只写打开
O_RDWR 可读可写打灶含开
以下可选项可以同时指定0个或多个,和必选项按位或起来作为flags 参数。可选项有很多,这
里只介绍一部分,其它选项可参考open(2)的Man Page:
O_APPEND 表示追加。如果文件已有内容,这次打开文件所写的数据附加到文件的末尾而不
覆盖原来的内容。
O_CREAT 若此文件不存在则创建它。使用此选项时需要提供第三个参数mode ,表示该文件
的访问权限。
O_EXCL 如果同时指定了O_CREAT,并且文件已存在,则出错返回。
O_TRUNC 如果文件已存在,并且以只写或可读可写方式打开,则将其长度截断
(Truncate)为0字节。
O_NONBLOCK 对于设备文件,以O_NONBLOCK 方式打开可以做非阻塞I/O(Nonblock I/O).
因毁哗返为第二个参数:O_WRON | O_CREAT
O_CREAT:如果打不开纤饥就创建
O_WRON | O_CREAT中间使用“|”,所以支持打不开就创建芦悔
在C中,常常使用二进制位来做控制标志,这个办法使得高效且代码短小,在头文件fcntl.h中,可以见到O_WRON的定义值是”01″,八位二进制就是””,O_CREAT是八进制”0100″,二进制就是””,竖线“|”昌键不是“与耐腊巧”,是逐位“或”运算,O_RWON|O_CREAT合起来就是”“,这两个”1″的局慎位置并不冲突,在这里,open()函数得到的值是编译器已经合并好了的值””,open()函数可以根据这两个独立的二进制”位”知道是读写打开或者创建。
这个是位或,不是与。位或、位与是这样计算的。
如:二进制的 010 | 001 结果是 011,而 010 & 001 结果就是0了。
O_WRON 和 O_CREAT 的关系就相当于上面的 010 和 001。他们位或的值喊帆不是0,位与的值就是0了。0表示什么都不做慧铅。用了位或后,就在一个整型的值上设置了不同的前渗好标志位,open函数会检测对应的标志位,如果该标志位设置为1了,就执行对应的操作。
O_CREAT的意思就是创建的意思,在这里就是将 创建文件 的标志位设置为1,这样open函数无法写这个文件的时候就会创建他。
#include
int open(const char *pathname, int oflag, … /*
mode_t mode */ );
We show the third argument as …, which is the ISO C way to specify that the number and types of the remaining arguments may vary. For this function, the third argument is used only when a new file is being created, as we describe later. We show this argument as a comment in the prototype.
The pathname is the name of the file to open or create. This function has a multitude of options, which are specified by the oflag argument. This argument is formed by ORing together one or more of the following constants from the header:
O_RDON
Open for reading only.
O_WRON
Open for writing only.
O_RDWR
Open for reading and writing.
Most implementations define O_RDON as 0, O_WRON as 1, and O_RDWR as 2, for compatibility with older programs.
One and only one of these three constants must be specified. The following constants are optional:
O_APPEND
Append to the end of file on each write. We describe this option in detail in Section 3.11.
O_CREAT
Create the file if it doesn’t exist. This option requires a third argument to the open function, the mode, which specifies the access permission bits of the new file. (When we describe a file’s access permission bits in Section 4.5, we’ll see how to specify the mode and how it can be modified by the umask value of a process.)
O_EXCL
Generate an error if O_CREAT is also specified and the file already exists. This test for whether the file already exists and the creation of the file if it doesn’t exist is an atomic operation. We describe atomic operations in more detail in Section 3.11.
O_TRUNC
If the file exists and if it is successfully opened for either write-only or readwrite, truncate its length to 0.
O_NOCTTY
If the pathname refers to a terminal device, do not allocate the device as the controlling terminal for this process. We talk about controlling terminals in Section 9.6.
O_NONBLOCK
If the pathname refers to a FIFO, a block special file, or a character special file, this option sets the nonblocking mode for both the opening of the file and subsequent I/O. We describe this mode in Section 14.2.
linux open o cloexec的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于linux open o cloexec,Linux开源o_cloexec功能,提升文件描述符处理效率,Linux环境编程:打开open函数创建的文件,然后对此文件进行读写操作。。。,linux c 的 open(文件路径,O_WRON | O_CREAT) 里面的与运算为什么可以实现打不开就创建的信息别忘了在本站进行查找喔。
香港服务器首选筋斗云,2H2G首月10元开通。
筋斗云()提供简单好用,价格厚道的香港/美国云服务器和独立服务器。IDC+ISP+ICP资质。ARIN和APNIC会员。成熟技术团队15年行业经验。