c - 打开使用 C 编写的文件时权限被拒绝

标签 c linux ubuntu permissions

关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。












此问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-topic在这里,这个问题的解决方式不太可能帮助 future 的读者。


去年关闭。







Improve this question




我正在使用以下代码对文件描述符进行一些简单的练习:

int main(int argc, char *argv[]){
    int fd1 = open("etc/passwd", O_RDONLY);
    int fd2 = open("output.txt", O_CREAT,O_TRUNC,O_WRONLY);

    dup2(fd1,0);
    close(fd1);

    dup2(fd2,1);
    close(fd2);
}
每当我尝试打开“output.txt”时,都会出现以下错误:
Unable to open 'output.txt': Unable to read file '/home/joao/Desktop/Exercicios/output.txt' (NoPermissions (FileSystemError): Error: EACCES: permission denied, open '/home/joao/Desktop/Exercicios/output.txt').
即使我相信某些错误与 VSCode 有关,我也无法在任何地方打开该文件。这是在包含 .c 文件、可执行文件和“output.txt”的文件夹上执行“ls -l”时得到的结果:
---------T 1 joao joao     0 jun  9 21:54 output.txt
-rwxrwxr-x 1 joao joao 16784 jun  9 21:54 test
-rw-rw-r-- 1  700 joao   387 jun  9 21:54 teste.c
我怎样才能解决这个问题?

最佳答案

这个:

int fd2 = open("output.txt", O_CREAT,O_TRUNC,O_WRONLY);
是不正确的。所有标志都放在第二个参数中,与按位或相结合,第三个是“模式”,即访问权限。见 the manual page更多细节,当然。
所以,它应该是:
const int fd2 = open("output.txt", O_CREAT | O_TRUNC | O_WRONLY, S_IRWXU);
这将以 S_IRWXU 模式打开,即仅授予所有者读/写/执行权限。

关于c - 打开使用 C 编写的文件时权限被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67911667/

相关文章:

php - drupal 7 如何让 cron 作业自动运行

linux - 不同的 perl verions 不共享安装的 rpms

ubuntu - 我应该为 Ubuntu 10.04 或 11.04 使用哪个 AWS AMI,并运行 node.js?

c - Linux内核中如何判断一个inode是目录还是mot

c++ - 如何知道C和C++中数据和代码的虚拟地址?

c++ - 使用 C++ 库在 Linux 中注册热键

linux - 它是相对路径还是绝对路径?

ubuntu - Jenkins 用户登录 - Ubuntu

c - 将简单的 HTML 解析成树

c - 如何在我的 Objective-C 头文件中定义一个数组?