c-fcntl和F_GETFL的意外返回值



从C.中的系统调用开始

目标->使用fcntl 获取文件的打开模式

编写的代码->

#include<stdio.h>
#include<fcntl.h>
int main()
{
int fd , open_fl;
if((fd =open("example.txt",O_RDWR ))<0){
perror("program");
}
open_fl = fcntl(fd , F_GETFL );
printf("%d file descriptor has %d flags.n", fd , open_fl);
return 0 ;
}

获取输出->

3 file descriptor has 32770 flags.

32770(十进制(->100002(八进制(

但是期望的八进制值是2。

源fcntl.h代码->https://github.com/torvalds/linux/blob/master/include/uapi/asm-generic/fcntl.h

为什么我会得到这样的输出差异?

您将返回o1000002,它是O_RDWR(o2(和O_LARGEFILE(o100000(标志的组合。

open((的手册页上写着:

O_LARGEFILE
(LFS) Allow files whose sizes cannot be represented in an
off_t (but can be represented in an off64_t) to be opened.
The _LARGEFILE64_SOURCE macro must be defined (before
including any header files) in order to obtain this
definition.  Setting the _FILE_OFFSET_BITS feature test
macro to 64 (rather than using O_LARGEFILE) is the
preferred method of accessing large files on 32-bit
systems (see feature_test_macros(7)).

相关内容

  • 没有找到相关文章

最新更新