这些答案中哪些等同于Ubuntu 14.04 LTS上的O_RDWR?

up9lanfz  于 2022-09-21  发布在  Unix
关注(0)|答案(5)|浏览(200)

我已经得到了以下复选框列表,其中包含了在Ubuntu 14.04LTS上选择相当于O_RDWR的所有真实答案问题的所有可能解决方案。根据下面的列表

O_RDONLY

1

1<<1

3和2

3|2

O_WRONLY

(O_RDONLY+O_WRONLY)

(O_RDONLY|O_WRONLY)

(O_RDONLY和O_WRONLY)

(O_RDONLY&O_WRONLY)

(O_RDONLY<<1)

(O_WRONLY<<1)

0

我已选择2和(O_RDONLY|O_WRONLY),但仍收到错误

有谁能帮我这个忙吗?

r1zhe5dt

r1zhe5dt1#

研究按位运算符。

O_RDWR is defined in fcntl.h and is equals to 2.
O_WRONLY = 1
O_RDONLY = 0

1 = 1
2 = 2
3 = 3
1 << 1 = 2
3 & 2 = 2
3 | 2 = 3
O_WRONLY = 1
(O_RDONLY + O_WRONLY) = 1
(O_RDONLY | O_WRONLY) = 1
(O_RDONLY & O_WRONLY) = 0
(O_RDONLY && O_WRONLY) = 0
(O_RDONLY << 1) = 1
(O_WRONLY << 1) = 2
0 = 0
628mspwn

628mspwn2#

都不是

O_RDWR的唯一等价物是O_RDWRO_RDWR不是单个位的逐位组合。O_EXECO_RDONLYO_SEARCHO_WRONLY也不是。

根据POSIX documentation for open()(加粗地雷):
..。应用程序应在OFLAG的值中指定以下前五个值(文件访问模式)中的**正好一个:

O_EXEC
    Open for execute only (non-directory files). The result is unspecified if this
    flag is applied to a directory.
O_RDONLY
    Open for reading only.
O_RDWR
    Open for reading and writing. The result is undefined if this flag is applied to a FIFO.
O_SEARCH
    Open directory for search only. The result is unspecified if this flag is
    applied to a non-directory file.
O_WRONLY
    Open for writing only.
flvlnr44

flvlnr443#

O_WRONLY=1、O_RDONLY=0和O_RDWR=2,所以按位运算,找出哪条语句得到2

你明白了吗?这意味着2,1<<1,3&2和(O_WRONLY<<1)这些可以给你2

9o685dep

9o685dep4#

2,1<<1也左移00000001=00000010所以,值也是2,3&2也是2,O_WRONLY<<1也是2,所以这是答案

jgovgodb

jgovgodb5#

答案是这样的:

  • []O_RDONLY
  • []1
  • [x]2
  • []3
  • [x]1<<1
  • [x]3和2
  • []3|2
  • []O_WRONLY
  • x
  • []0

相关问题