2016-03-30 31 views
1

2 dosyayı karşılaştıran ve eşit olup olmadıklarını gösteren bir program yazmaya çalışıyorum. fork, dup, dup2, open, write, exec ve read:bash: ./comp.out: İzin reddedildi

Sadece işlevlerini kullanabilirsiniz. Ben linux gcc programı derlerken

, bu döndürür:

bash: ./comp.out: İzin

yalanladı kodu:

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

Nasıl İzinleri değiştirir miyim?

[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

Çalıştırmak sudo./Comp.out', belki? –

+0

Ancak terminalde komutları kullanamıyorum. –

+0

Ne demek istiyorsun? Bunları bir sebepten dolayı kullanma izniniz yok mu? –

cevap

3

komut

gcc -c ec11.c -o  comp.out 

bir nesne dosyası değil, yürütülebilir bir program dosyası oluşturur. Nesne dosyaları derlenmiş translation units (tüm üstbilgi dosyaları içeren kabaca kaynak dosya) ve daha fazlası değil. -c bayrağı, bir nesne dosyası oluşturmak için derleyici ön program gcc bildirir, bu nedenle ya -c bayrağını kaldırın veya açıkça bağlantı. böylece derleyici size şeyleri daha fazla uyarı verecektir derlerken

Yani ya

$ gcc ec11.c -o comp.out 

Ya alakasız bir kayda göre

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

, ben uyarı bayrakları eklemenizi tavsiye Çalışırken sorun yaratabilir (tanımlanmamış davranışlar veya mantıksal/anlamsal sorunlar gibi). Ben şahsen en az -Wall -Wextra -pedantic bayraklarını kullanıyorum.

+0

Ama şimdi farklı bir sorun var diyor _shay @ shay-Latitude-E6410 ~/çalışma alanı/targ1OS $ ./comp.out input.txt input.txtKanal okumak giriş dosyası_ –

+1

@ShayChercavsky Bu başka bir soru için başka bir sorundur. –

İlgili konular