2010-11-29 166 views
0
int fd = open(JOYSTICK_NAME, O_RDONLY | O_NONBLOCK); 

O_RDONLYO_NONBLOCK之間的酒吧是什麼意思?我在OpenGL/GLUT編程中遇到過這個問題,我很好奇它的語義。C++酒吧功能參數

+0

你可能想看看一些基本的C++文本。在這裏看到一個由C++人認爲是好的列表:http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – 2010-11-29 16:40:19

回答

3

這是bitwise OR operator。它採用O_RDONLY中的各個位並將它們與O_NONBLOCK中的位組合,並返回組合值。

例如,假設二進制值O_RDONLY是11001100和O_NONBLOCK二進制值是00001111.的OR-ing這些結合在一起產生11001111.

2

這是按位運算符。它用來累積比特位。

2

這是兩個操作數的bitwise OR。在這種情況下,操作數都在fcntl.h定義:

/* File access modes for open() and fcntl(). POSIX Table 6-6. */ 
#define O_RDONLY   0 /* open(name, O_RDONLY) opens read only */ 
#define O_WRONLY   1 /* open(name, O_WRONLY) opens write only */ 
#define O_RDWR    2 /* open(name, O_RDWR) opens read/write */ 
... 
/* File status flags for open() and fcntl(). POSIX Table 6-5. */ 
#define O_APPEND  02000 /* set append mode */ 
#define O_NONBLOCK  04000 /* no delay */ 

所以O_RDONLY

000 000 000 000 (0) 

被進行或運算與O_NONBLOCK

100 000 000 000 (04000 in octal notation) 

結果因此是:

100 000 000 000 (0400) 

不是一個非常令人興奮的例子,但它就是這樣做的...

1

這是一個按位或。它採用兩個參數(O_RDONLY和O_NONBLOCK)的二進制表示形式,並將OR操作應用於它們,並返回結果。