open函数的问题,请教各位大神啊!!!!!!!!!!

thlcly 2013-11-07 04:49:11
在Linux RedHat中,为什用open函数以只读方式打开一个文件而是可以成功,并且还会创建文件??
不是应该加了O_CREAT之后才会创建文件的吗??请教各位大神帮助小弟解答
以下是代码

#include<stdio.h>
#include<stdlib.h>

#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>

int main(int argc,char **argv)
{
int fd=0;
if(argc<2)
{
puts("please input the open file pathname!\n");
exit(1);
}

if((fd=open(argv[1],O_RDONLY))<0)
{
perror("open file failure!\n");
exit(1);
}
else
{
printf("open file %d success!\n",fd);
}
close(fd);
exit(0);
}
...全文
115 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
thlcly 2013-11-14
  • 打赏
  • 举报
回复
关闭虚拟机冲洗打开后就好了,可能是系统本身的问题
xiaoli96123 2013-11-08
  • 打赏
  • 举报
回复
是不是这个文件已经有了,你没看见呢?
max_min_ 2013-11-07
  • 打赏
  • 举报
回复
不会吧!把文件删掉再运行试试!

The  argument  flags  must  include  one  of  
the  following  access  modes: O_RDONLY, O_WRONLY, or O_RDWR.  
These request opening the file read-only, write-only, or read/write, respectively.
beginning1126 2013-11-07
  • 打赏
  • 举报
回复
不会啊。。我是centos的,运行打开失败,没看到创建文件啊

23,124

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧