gpt4 book ai didi

c - open() 没有正确设置文件权限

转载 作者:太空狗 更新时间:2023-10-29 16:51:28 27 4
gpt4 key购买 nike

<分区>

我使用以下代码创建一个文件:

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>

int main()
{
const char* filename = "./test.out";
int fd;
if(-1 == (fd = open(filename, O_CREAT|O_RDWR, 0666)))
{
perror("Error");
errno = 0;
}
else
puts("File opened");

if(-1 == (close(fd)))
{
perror("Error");
errno = 0;
}
else
puts("File closed");

return 0;
}

我将 mode 参数指定为 0666,这应该授予所有人读、写访问权限。但是,ls -l 显示

-rw-r--r-- 1 kmehta 用户 0 2012-01-29 16:29 test.out

如您所见,写入权限仅授予文件的所有者。我不知道为什么其他人没有被正确授予权限。 chmod a+w test.out 正确设置了权限。

代码编译为gcc -Wall test.c

规范:Opensuse 11.3 64 位上的 gcc v 4.5.0

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com