程序:
#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
调用您的程序以使其成功。
还要始终检查函数的返回值,如 open
和 fchmod
并处理错误。
关于c - C 中的 fchmod 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32537412/