2017-04-08 33 views
2

您好,晚上好,打印內容目錄中的C語言編程

我正在做一個程序,將收集的所有文件在給定的目錄,並打印出所有的整數裏面他們。

這是作業,但不是作業。這是我無法弄清楚的一小部分。

下面的代碼:

#include <stdio.h> 
#include <string.h> 
#include <sys/types.h> 
#include <dirent.h> 
#include <unistd.h> 
#include <stdlib.h> 
#include "mergefiles.c" 

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

if(2 != argc) 
{ 
    printf("Directory name missing!\n"); 
    return 0; 
} 
DIR *dp = NULL; 
struct dirent *dptr = NULL; 
FILE *input_file; 
FILE *next_file; 
char c; 

if (NULL == (dp = opendir(argv[1]))) 
{ 
    printf("Cannot open the given directory %s", argv[1]); 
    exit(1); 
} 
else 
{ 
    while(NULL != (dptr = readdir(dp))) 
    { 
     if(!strcmp (dptr->d_name, ".")) 
     { 
      continue; 
     } 
     if(!strcmp (dptr->d_name, "..")) 
     { 
      continue; 
     } 

     input_file = fopen(dptr->d_name, "r"); 
     if(input_file == NULL) 
     { 
      perror("Cannot open file or no file exists\n"); 
      fclose(input_file); 

      return 0; 
     } 
     c = fgetc(input_file); 
     while(c != EOF) 
     { 
      printf("%d", c); 
     } 
     printf(%s\n", dptr->d_name); 
    } 
    closedir(dp); 
} 
return 0; 
} 

我開始用這個慢。首先,我讀了一個目錄。然後我打印目錄中所有文件的名稱。所有這些工作都很好(這真的很酷!)。

但是,每當我嘗試在第一個while loop內執行fclose(input_file)時,我會收到一個segfault。起初,我認爲這是因爲文件中沒有任何東西(我通過在文件中加入東西來解決這個問題),但這不起作用。

奇怪的是,當我拿出fclose(input_file)時,它會編譯,但拋出第二個錯誤:Cannot open file or file doesn't exist

所有文件都包含整數,每行一個,深度不等。我盡我所能來校對代碼,如果有任何錯誤(錯字等)讓我知道,我會馬上編輯它。

謝謝你們所有的智慧。 :d

更新:

所以我得到一個segfault因爲input_fileNULL,我試圖關閉它...這是有道理的,我不能相信我錯過了。

我不明白的是爲什麼input_fileNULL擺在首位。

+0

您正在將NULL傳遞給fclose。您需要將該調用移至讀取完畢的位置,而不是將NULL測試爲input_file的位置。 – bruceg

+0

那麼,解決這個謎,但爲什麼input_file首先是NULL? – WarrenJB

+1

你的代碼顯示'if(input_file == NULL)' - 顯然,如果'input_file == NULL',你不能'fclose(input_file)';你在結束什麼?空值。如果打開文件失敗,Input_file將爲NULL。開放可能會由於多種原因而失敗(例如,僅由另一個進程打開),但爲什麼失敗與您的問題無關。你的代碼有一個缺陷,它明顯地試圖關閉你已經知道是NULL的東西。 –

回答

0

你爲什麼得到input_file等於NULL是很明顯的,因爲沒有你想要打開的文件在當前目錄中。

假設例如考慮您的工作目錄(這是你在哪裏編譯你的C程序)

/home/student/Myfolder

讓我們假設你要打開「輸入」文件夾,有兩個文件1.txt的和2.txt。

編譯什麼編譯器爲fopen()函數後是它搜索文件

/home/student/Myfolder/1.txt

但實際的路徑是

/home/student/Myfolder/Input/1.txt

我希望你明白這一點。爲了克服這個問題,你必須得到當前工作目錄cwd()函數,然後附加目錄名和文件名。以下是完整的程序。請記下我在程序中所做的命令。

#include <stdio.h> 
#include <string.h> 
#include <sys/types.h> 
#include <dirent.h> 
#include <unistd.h> 
#include <stdlib.h> 

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

if(2 != argc) 
{ 
    printf("Directory name missing!\n"); 
    return 0; 
} 
DIR *dp = NULL; 
struct dirent *dptr = NULL; 
FILE *input_file; 
FILE *next_file; 
char c,cwd[256]; //created new character array 

if (NULL == (dp = opendir(argv[1]))) 
{ 
    printf("Cannot open the given directory %s", argv[1]); 
    exit(1); 
} 
else 
{ 
    while((dptr = readdir(dp))!=NULL) 
    { 
     if(!strcmp (dptr->d_name, ".")) 
     { 
      continue; 
     } 
     if(!strcmp (dptr->d_name, "..")) 
     { 
      continue; 
     } 
    if(getcwd(cwd, sizeof(cwd)) == NULL) //this is important 
    { 
     printf("No such file or directory"); 
     continue; 
    } 
    if(dptr->d_name[0]=='.') //Files never begin with '.' 
     continue; 
    strcat(cwd,"/"); //For windows "\" 
    strcat(cwd,argv[1]); 
    strcat(cwd,"/"); //For windows "\" 
    strcat(cwd,dptr->d_name); 
    printf("%s",cwd); //check for working directory 
     input_file = fopen(cwd, "r"); 
    printf("%s\n",dptr->d_name); //validation check 
     if(input_file == NULL) 
     { 
      perror("Cannot open file or no file exists\n"); 
      fclose(input_file); 
      return 0; 
     } 
     c = fgetc(input_file); 
     while(c != EOF) 
     { 
      printf("%d", c); 
     } 
     printf("%s\n", dptr->d_name); 
    } 
    closedir(dp); 
} 
return 0; 
}