open 创建文件并读写的错误--bad file descriptor
<div style="font-family: Arial, Verdana, sans-serif; margin-top: 5px; margin-right: 5px; margin-bottom: 5px; margin-left: 5px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px;">昨天准备做一个程序,用来产生有 holes 的文件,于是用 open 来新建一个文件:
fd = open ("hole", O_CREAT, S_IRWXU | S_IRWXG) ;
然后准备用 write 写入数据,结果却出错了:Bad file descriptor.
郁闷地查了很久,将近一个小时之后,通过不断地对比书上的代码,猛然想到一个问题:
“O_CREAT 只是创建了一个文件,并未确定这个文件的状态。”
于是赶紧补上一个 O_RDWR:
fd = open ("hole", O_CREAT | O_RDWR, S_IRWXU | S_IRWXG) ;
果然接下来就没有任何问题了……
页:
[1]