gpt4 book ai didi

c - linux 打开标志中的按位或

转载 作者:IT王子 更新时间:2023-10-29 00:40:38 26 4
gpt4 key购买 nike

在linux open 系统调用中,flags 中的按位或是什么意思。这是编译器如何解释的。这是一个例子:

fd = open("myfile", O_RDONLY | O_CREAT | O_TRUNC, S_IRUSR);

此外,逗号运算符在标志中有什么作用?

更新:如果我们使用 && 运算符

,使用其他运算符会产生什么效果?

最佳答案

How this is interpreted by the compiler

与任何其他按位或运算没有区别。考虑以下 #define,例如在 /usr/include/asm-generic/fcntl.h 中找到的(注意值是八进制的):

#define O_RDONLY        00000000
#define O_CREAT 00000100
#define O_TRUNC 00001000

然后,在您的示例中,传递给函数的值为 00000000 | 00000100 | 0000100000001100。通过评估各个位的位置,open() 可以重建调用者设置了哪些标志:

if (oflag & O_CREAT) {
/* caller wants the file to be created */
}

if (oflag & O_TRUNC) {
/* caller wants the file to be truncated */
}
...

关于c - linux 打开标志中的按位或,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22008229/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com