k&r fopen and fillbuf

谁都会走 提交于 2019-12-11 08:07:12

问题


I have a question regarding the other question asked on stack overflow:- segmentation fault on c K&R fopen and fillbuf. In this question there is a discussion of flags in struct _iobuf. These are used as different access modes. But down here in enum flags there are are some particular values of flags. So will 'flag' take only these particular values or these are some standard values whereas flag can take some other values as well? My doubt arises from the fact that while defining the array _iob only the three standard values (for stdin, stdout, stderr) were given(out of 20) so the fp can take some other values as well(17 others at the same time). The second doubt is that if flag can take only the defined values like(_READ,_WRITE etc.), then in int _fillbuf() function in place of writing

if((fp->flag & (_READ|_EOF|_ERR))!=_READ)

can we write as

if((fp->flag==_WRITE || fp->flag== _UNBUF))

because out of the given fixed flag values it still makes sense.


回答1:


The enum values are flag bits and the flag member can have several of them set. (Not all combinations are meaningful, but many are; the __READ, __EOF and __ERR flags are independent of each other and all eight combinations are possible.).

Consequently

if((fp->flag & (_READ|_EOF|_ERR))!=_READ)

Tests that the file is open for reading and has neither the error nor the EOF flags set.



来源:https://stackoverflow.com/questions/50598201/kr-fopen-and-fillbuf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!