
linux mmap() 函数出现 Permission denied
#include<stdio.h>#include<sys/mman.h>#include<sys/types.h>#include<sys/stat.h>#includ...
#include<stdio.h>
#include<sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main()
{
int fd=open("./a.txt",O_WRONLY);
if(fd<0)perror("open failed:");
int* p=mmap(
NULL,
10*sizeof(int),
PROT_READ|PROT_WRITE,
MAP_PRIVATE,
fd,
0
);
printf("p=%p,fd=%d\n",p,fd);
if(p==MAP_FAILED){
perror("failed:");
return -1;
}
int i;
for(i=0;i<10;i++){
p[i]=i+5;
}
return 0;
} 展开
#include<sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main()
{
int fd=open("./a.txt",O_WRONLY);
if(fd<0)perror("open failed:");
int* p=mmap(
NULL,
10*sizeof(int),
PROT_READ|PROT_WRITE,
MAP_PRIVATE,
fd,
0
);
printf("p=%p,fd=%d\n",p,fd);
if(p==MAP_FAILED){
perror("failed:");
return -1;
}
int i;
for(i=0;i<10;i++){
p[i]=i+5;
}
return 0;
} 展开
2个回答
展开全部
int fd=open("./a.txt",O_WRONLY); //这个地方错了。你应该用读写方式打开。而不是只写方式。
比如:int fd=open("./a.txt",O_RDWR); 这样改后就不会报错了。
比如:int fd=open("./a.txt",O_RDWR); 这样改后就不会报错了。

2022-08-05 广告
苏州蓝晓生物科技有限公司。标准化核心产品:公司拥有完整的琼脂糖介质、葡聚糖介质、聚甲基丙烯酸酯介质生产线,年产分离介质50000L,产品质量稳定并达到国际领先水平。核心优势:公司核心技术人员拥有近二十年不同基质的基球开发和官能化的丰富技术经...
点击进入详情页
本回答由苏州蓝晓生物科技有限公司_提供
展开全部
你到底想解决一个什么样的问题?从你的话语中,就知道你的程序出现了“Permission denied”。
补充一句,既然出现了这句,有可能是执行这个程序的用户没有使用某个资源(比如:读取一个文件)的权限。
补充一句,既然出现了这句,有可能是执行这个程序的用户没有使用某个资源(比如:读取一个文件)的权限。
更多追问追答
追问
改成777了,还是不行。。。chmod a=rwx a.txt
追答
你把原文件的权限属性改成777,并不等于这个程序能运行,这个是C程序,要编译后才能运行!
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |