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

标签 c linux

在我的 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/

相关文章:

c++ - 如果 lt_dlopen 失败,如何获取更多信息?

linux - 使用&&和||时如何执行exit命令?

c - Windows 上的 hidapi - 对函数的 undefined reference

objective-c - 如何获取二维数组中的char数组元素

linux - Linux 上外部 Web (Grails) 应用程序配置文件的标准位置

ruby-on-rails - 目录 : $bundle install =>/usr/bin/env: ruby: not a directory

linux - 在 Linux 中对客户端断开连接使用react

c - 解析分块的 HTTP/1.1 响应

c++ - 如何在 UML 事件图中表示编译时条件?

c - C 中的二进制表示