关闭。这个问题是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/