gpt4 book ai didi

c - O_RDWR 权限被拒绝

转载 作者:太空宇宙 更新时间:2023-11-03 23:31:13 24 4
gpt4 key购买 nike

我创建了这个文件

char *output = "big";
creat(output, O_RDWR);

当我尝试读取文件时

 cat big

我的权限被拒绝了。我的代码有什么问题?如何创建具有读写权限模式的文件?

用ls -l,big的权限是这样的

----------

这是什么意思?

最佳答案

您误解了模式参数。从手册页:

          mode specifies the permissions to use in case a new file is cre‐
ated. This argument must be supplied when O_CREAT is specified
in flags; if O_CREAT is not specified, then mode is ignored.
The effective permissions are modified by the process's umask in
the usual way: The permissions of the created file are
(mode & ~umask). Note that this mode only applies to future
accesses of the newly created file; the open() call that creates
a read-only file may well return a read/write file descriptor.

还有

   creat()    is    equivalent    to    open()   with   flags   equal   to
O_CREAT|O_WRONLY|O_TRUNC.

因此,更合适的调用可能如下所示:

int fd = creat(output, 0644); /*-rw-r--r-- */

如果你想打开它O_RDWR,那么只需使用open():

int fd = open(output, O_CREAT|O_RDWR|O_TRUNC, 0644);

关于c - O_RDWR 权限被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15013107/

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