对/tmp文件打开失败

fay8048 2011-03-07 11:48:17
先贴测试代码
#include <sys/types.h>
2 #include <sys/stat.h>
3 #include <fcntl.h>
4 #include <unistd.h>
5
6
7 int main()
8 {
9 int res;
10 res = open ("/tmp/test", O_RDONLY);
11 if (res == -1)
12 {
13 write (1, "wrong\n", 4);
14 }
15 else
16 {
17 write (1, "open\n", 5);
18 }
19 }
这个程序就是打开在临时文件夹打开一个文件
但res总是为-1
就是打开错误,不明白
...全文
125 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
ericming200409 2011-03-08
  • 打赏
  • 举报
回复
http://blog.csdn.net/ericming200409/archive/2011/01/24/6162152.aspx
fay8048 2011-03-08
  • 打赏
  • 举报
回复
呵呵,看来着就是高手的魅力?,刚我也想到权限问题,并且进超级用户运行程序,真奇怪,这下就行啦,
问题解决啦,小弟刚学linux程序设计,
bluejays 2011-03-08
  • 打赏
  • 举报
回复
/tmp/test这个文件是否存在?
用 "cat /tmp/test" 或者 "vi /tmp/test"能否察看?
如果可以,那么同一个用户用这段代码应该也可以打开
bluejays 2011-03-07
  • 打赏
  • 举报
回复
代码本身貌似没问题。看看open之后的errno是多少吧
#include <errno.h>
#include <string.h>
#include <stdio.h>
。。。。。。
if (res == -1)
{
write (1, "wrong\n", 4);
printf("\n%d : %s\n", errno, strerror(errno));
}
fay8048 2011-03-07
  • 打赏
  • 举报
回复
题目说错啦,对这个目录能打开,但是里面的文件不能
是不是发行办考虑安全的问题,高手解答,用的是ubuntu
fay8048 2011-03-07
  • 打赏
  • 举报
回复
题目说错啦,对这个目录能打开,但是里面的文件不能
是不是发行办考虑安全的问题,高手解答

23,116

社区成员

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

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