In section 8.5, in fopen example, K&R write:
if ((fp->flag & (_READ | _EOF | _ERR )) != _READ)
But I see this easier:
if (fp->flag != _READ)
Is my code bad or K&R is simple obscure... Thanks
In section 8.5, in fopen example, K&R write:
if ((fp->flag & (_READ | _EOF | _ERR )) != _READ)
But I see this easier:
if (fp->flag != _READ)
Is my code bad or K&R is simple obscure... Thanks
The bits of the flag
member are assigned more meanings than just _READ
(01
), _EOF
(010
, which is octal for 8), and _ERR
(020
, 16). There are also _WRITE
(02
) and _UNBUF
(04
).
Kernighan and Ritchie’s code, (fp->flag & (_READ | _EOF | _ERR )) != _READ
, is true if and only if _READ
is clear, _EOF
is set, or _ERR
is set.
Your code, fp->flag != _READ
, is true if and only if _READ
is clear, _EOF
is set, _ERR
is set, _WRITE
is set, or _UNBUF
is set.
Thus, for example, if _UNBUF
is set while _READ
is set, _EOF
is clear, and _ERR
is clear, the Kernighan and Ritchie code will evaluate to false but your code will evaluate to true. The Kernighan and Ritchie code asks only about three flags and does not care about the other flags. Your code is affected by all the flags.