c - C 中的 fchmod 函数

标签 c linux unix chmod

程序:

#include<stdio.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<fcntl.h>
void main()
{
    int fd=open("b.txt",O_RDONLY);
    fchmod(fd,S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH);
}

输出:

$ ls -l b.txt
----r----- 1 mohanraj mohanraj 0 Sep 12 15:09 b.txt
$ ./a.out
$ ls -l b.txt
----r----- 1 mohanraj mohanraj 0 Sep 12 15:09 b.txt  
$

对于上述程序,我预期的输出是将 b.txt 的权限设置为“rw_rw_r__”。但是,它仍然保留在旧的 允许。为什么会这样。这段代码有错误吗?

最佳答案

您没有修改文件的权限,请使用 sudo 调用您的程序以使其成功。

还要始终检查函数的返回值,如 openfchmod 并处理错误。

关于c - C 中的 fchmod 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32537412/

相关文章:

c - Pthread 互斥锁断言失败

linux - TASK_KILLABLE 和 TASK_INTERRUPTIBLE 有什么区别?

linux - GCC - 不要编译未使用的代码

python - 如何在MATLAB的Conda环境中运行Python脚本?

c - C 中的高级数据结构

c - 在 xinu 中恢复中断 128 0x80

Windows/Unix 服务器上的 JAVa IO 限制导致应用程序出现未知问题

c++ - setenv, unsetenv, putenv

c - 管理 C 中的内存,二维 float 组

linux - 如何在非 root Informix 安装上向 OS 用户授予 DBSA 等效权限?