gpt4 book ai didi

c - bash : ./comp.out:权限被拒绝

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:49:26 27 4
gpt4 key购买 nike

我正在尝试编写一个程序来比较 2 个文件并返回它们是否相等。

我只能使用函数:forkdupdup2openwrite execread

当我在 linux gcc 上编译程序时,它返回:

bash: ./comp.out: Permission denied

代码:

#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

int CheckSimilar(char *path1, char *path2);

int main(int argc, char **argv) {

int returnValue = CheckSimilar(argv[1], argv[2]);

switch (returnValue){

case 1:
write(1, "1\n", 3);
break;
case 2:
write(1, "2\n", 3);
break;
case 3:
write(1, "3\n", 3);
break;
default:
break;
}

return 0;
}

/*
* This function checks if the files are similar or similar by case sensitive
* it gets 2 files, and returns: 3 if identical, 2 if identical but only if not
* case sensitive or 1 else.
*/

如何更改权限?

shay@shay-Latitude-E6410 ~/workspace/targ1OS $ gcc -c ec11.c -o      comp.out
shay@shay-Latitude-E6410 ~/workspace/targ1OS $ ls
comp.out Debug ec11.c
shay@shay-Latitude-E6410 ~/workspace/targ1OS $ ./comp.out /home/shay/Downloads/input.txt /home/shay/Downloads/input.txt
bash: ./comp.out: Permission denied
shay@shay-Latitude-E6410 ~/workspace/targ1OS $ comp.out /home/shay/Downloads/input.txt /home/shay/Downloads/input.txt
comp.out: command not found
shay@shay-Latitude-E6410 ~/workspace/targ1OS $ ./comp.out /home/shay/Downloads/input.txt /home/shay/Downloads/input.txt
bash: ./comp.out: Permission denied
shay@shay-Latitude-E6410 ~/workspace/targ1OS $ ^C
shay@shay-Latitude-E6410 ~/workspace/targ1OS $ ls -al ./comp.out
-rw-r--r-- 1 shay shay 2640 Mar 30 10:05 ./comp.out
shay@shay-Latitude-E6410 ~/workspace/targ1OS $ chmod ogu+x ./comp.out
shay@shay-Latitude-E6410 ~/workspace/targ1OS $ ./comp.out /home/shay/Downloads/input.txt /home/shay/Downloads/input.txt
bash: ./comp.out: cannot execute binary file: Exec format error
shay@shay-Latitude-E6410 ~/workspace/targ1OS $ gcc -c ec11.c -o comp.out
shay@shay-Latitude-E6410 ~/workspace/targ1OS $ ./comp.out /home/shay/Downloads/input.txt /home/shay/Downloads/input.txt
bash: ./comp.out: Permission denied
shay@shay-Latitude-E6410 ~/workspace/targ1OS $ chmod ogu+x ./comp.out
shay@shay-Latitude-E6410 ~/workspace/targ1OS $ ./comp.out /home/shay/Downloads/input.txt /home/shay/Downloads/input.txt
bash: ./comp.out: cannot execute binary file: Exec format error
shay@shay-Latitude-E6410 ~/workspace/targ1OS $ comp.out /home/shay/Downloads/input.txt /home/shay/Downloads/input.txt
comp.out: command not found

最佳答案

命令

gcc -c ec11.c -o      comp.out

创建一个目标文件,而不是一个可执行程序文件。目标文件是编译的 translation units (包含所有头文件的大致源文件)仅此而已。 -c 标志告诉编译器 fronteng 程序 gcc 创建目标文件,因此要么删除 -c 标志,要么显式链接.

所以要么

$ gcc ec11.c -o comp.out

或者

$ gcc -c ec11.c
$ gcc ec11.o -o comp.out

另外,我建议您在编译时添加警告标志,这样编译器会针对可能在运行时引起问题的事情(例如未定义的行为或逻辑/语义问题)向您发出更多警告。我个人至少使用标志 -Wall -Wextra -pedantic

关于c - bash : ./comp.out:权限被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36302130/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com