這是我的代碼:在C mmap中寫入文件:Permission denied。 Linux的
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
int main()
{
int fd=open("/home/victor/hello",O_WRONLY);
if(fd<0)
{
perror("Open");
exit(EXIT_FAILURE);
}
struct stat sbuf;
if(fstat(fd, &sbuf)==-1){
perror("stat");
close(fd);
exit(EXIT_FAILURE);
}
void* file_memory= mmap(NULL, sbuf.st_size, PROT_WRITE, MAP_SHARED,fd,0);
if (file_memory == MAP_FAILED) {
perror("Error mmapping the file");
close(fd);
exit(EXIT_FAILURE);
}
return 0;
}
我想這太
int fd=open("/home/victor/hello",O_WRONLY|0777);
但它同樣的錯誤:
錯誤mmapping文件:權限被拒絕
做ls -l | grep hola -rwxrwxrwx 1勝者勝者24 oct 24 01:47你好
怎麼了?
AFAIK'MMAP()'的'PROT_WRITE'可以暗示'PROT_READ','PROT_READ'與'open()'不兼容,使用'O_WRONLY'。 –
@IwillnotexistIdonotexist:做出答案。這是正確的。 –
@Iwillnotexist Idonotexist謝謝! 我改變了我的open()O_WRONLY for O_RDWR,一切都很好! –