2016-03-30 19 views
1

Ich versuche, ein Programm zu schreiben, das 2 Dateien vergleicht und zurückgibt, wenn sie gleich sind oder nicht.bash: ./comp.out: Berechtigung verweigert

Ich kann nur die Funktionen zur Verfügung: fork, dup, dup2, open, write, exec und read.

Wenn ich das Programm unter Linux gcc kompilieren, gibt es:

bash: ./comp.out: Erlaubnis verweigert

der Code:

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

Wie kann Ich ändere die Berechtigungen?

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

Führen Sie 'sudo./Comp.out', vielleicht? –

+0

Aber ich kann keine Befehle auf dem Terminal verwenden. –

+0

Was meinst du? Darf man sie aus irgendeinem Grund nicht benutzen? –

Antwort

3

Der Befehl

gcc -c ec11.c -o  comp.out 

schafft eine Objektdatei, nicht um eine ausführbare Programmdatei. Objektdateien sind die kompilierten translation units (ungefähr Quelldatei mit allen enthaltenen Header-Dateien) und nichts mehr. Es ist die -c Flags, die das Compiler-Front-Programm gcc anweist, eine Objektdatei zu erstellen, also entfernen Sie entweder das -c Flag, oder explizit verknüpfen.

Also entweder

$ gcc ec11.c -o comp.out 

Oder

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

auf einem nicht verwandten Notiz, ich Sie beraten Warnflaggen hinzuzufügen, wenn damit der Compiler kompiliert werden Sie mehr Warnungen über Dinge geben das könnte Probleme beim Ausführen verursachen (wie undefiniertes Verhalten oder logische/semantische Probleme). Ich persönlich benutze mindestens die Flaggen -Wall -Wextra -pedantic.

+0

Aber jetzt habe ich ein anderes Problem es sagt _shay @ shay-Latitude-E6410 ~/workspace/targ1OS $ ./comp.out input.txt input.txtCannot Eingabedatei_ –

+1

@ShayChercavsky Das ist ein weiteres Problem für eine andere Frage. –

Verwandte Themen