gpt4 book ai didi

c - 使用 O_CREAT|O_RDWR 打开文件将导致文件为只写

转载 作者:太空宇宙 更新时间:2023-11-04 08:13:14 30 4
gpt4 key购买 nike

在我的 C 代码中,我使用 open() 和选项 O_CREAT|O_RDWR 写入文件,然后是 write():

    readfd = open("ak.bin", O_CREAT|O_RDWR);
if (readfd >= 0) {
res = write(readfd, key, 16);
close(readfd);
}

在执行我的程序后,我发现文件"ak.bin" 只有写权限。我看不懂!

$ ls -l | grep ak
-----w---- 1 mohamed mohamed 16 Jun 3 18:30 ak.bin

我缺少什么?我希望我的文件也可读。

最佳答案

您应该在创建 (O_CREAT) 文件时指定模式

来自男人open(2) :

The mode argument specifies the file mode bits be applied when a new file is created. This argument must be supplied when O_CREAT or O_TMPFILE is specified in flags; if neither O_CREAT nor O_TMPFILE is specified, then mode is ignored. The effective mode is modified by the process's umask in the usual way: in the absence of a default ACL, the mode of the created file is (mode & ~umask). Note that this mode applies only 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.

关于c - 使用 O_CREAT|O_RDWR 打开文件将导致文件为只写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37620454/

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