Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
bash:./comp.out:权限被拒绝_C_Linux_Gcc_Permissions - Fatal编程技术网

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

bash:./comp.out:权限被拒绝,c,linux,gcc,permissions,C,Linux,Gcc,Permissions,我正在尝试编写一个程序,比较两个文件,如果它们相等或不相等,则返回结果 我只能使用以下功能:fork,dup,dup2,open,write,exec和read 当我在linux gcc上编译程序时,它返回: bash:./comp.out:权限被拒绝 守则: #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> int CheckSimilar(char *path1, char *pat

我正在尝试编写一个程序,比较两个文件,如果它们相等或不相等,则返回结果

我只能使用以下功能:
fork
dup
dup2
open
write
exec
read

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

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

守则:

#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.
*/
命令

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

所以

$ gcc ec11.c -o comp.out



另一方面,我建议您在编译时添加警告标志,以便编译器在运行时向您提供有关可能导致问题的更多警告(如未定义的行为或逻辑/语义问题)。我个人至少使用了标志
-Wall-Wextra-pedantic

Run
sudo./comp.out
,也许吧?但我不能在终端上使用命令。@ShayChercavsky你是真的在将源代码编译成可执行程序,还是只是试图运行源文件?或者您可能正在创建一个对象文件?您需要向我们展示如何构建源代码来创建程序。@ShayChercavsky然后向我们展示如何做到这一点。编辑您的问题以显示一个或多个命令,包括所有标志。顺便说一下,在一个不相关的注释上,您应该真正检查程序是否有两个参数,否则您将有未定义的行为,因为您可能会使用
argv
越界。在调用前检查例如
if(argc<3){printf(“对于少数参数,必须有两个参数”);返回1;}
shay@shay-Latitude-E6410~/workspace/targ1OS$./comp.out input.txt input.txt无法读取输入file@ShayChercavsky这是另一个问题的另一个问题。
$ gcc ec11.c -o comp.out
$ gcc -c ec11.c
$ gcc ec11.o -o comp.out