2017-03-02 62 views
0

爲什麼我的S_ISDIR和S_ISREG不能返回正確的目錄/文件名?爲什麼我的S_ISDIR和S_ISREG不能返回正確的目錄/文件名?

當我使用一個測試目錄運行我的程序時,我使用其他3個目錄和一個文本文件進行測試,只有Dir的printf()執行。

#include "dirAssignment.h" 

//Struct 
typedef struct directories{ 

}directories; 


//Method to look for directories and files: 
void search(const char *fileName){ 
    struct stat dirInfo; 
    struct dirent *dirp; 
    DIR *dp; 

    //Checks to see if there is an error with the fileName: 
    if (lstat(fileName, &dirInfo) < 0) 
     { 
     //Error: 
     printf("Something went wrong!\n"); 
     printf("%s\n",strerror(errno)); 
    } 

    //Checks to see if file can be opened: 
    if ((dp = opendir(fileName)) == NULL) 
    { 
     printf("Cannot open file %s\n", fileName); 
     printf("%s\n", strerror(errno)); 
    } 

    //If file can be opened, get all the files from the directory: 
    while((dirp = readdir(dp)) != NULL) 
    { 
     //Check for(and Ignore) parent and self: 
     if (strcmp(dirp->d_name,".") == 0 || strcmp(dirp->d_name,"..") == 0)   
     { 
      continue; 
     } 
     printf("%s:\t",dirp->d_name); 


     if(S_ISDIR(dirInfo.st_mode)) 
     { 
      printf("Dir\n"); 
     } 
     else if(S_ISREG(dirInfo.st_mode)) 
     { 
      printf("File\n"); 
     } 
    /* 
     else if(S_ISDIR(dirp->d_name, &dirInfo)) 
     { 
      //stat(fileName, &dirInfo); 

      //Checks to see if the file is a directory: 
      if (S_ISDIR(getcwd(dirp->d_name,512).st_mode)) 
      { 
       printf("directory in: %s\n", fileName); 
      } 
      //Must be a file: 
      else if (S_ISREG(getcwd(dirp->d_name,512).st_mode)) 
      { 
       printf("file in: %s\n", fileName); 
      } 
      //No clue how you got here! 
      else 
      { 
       printf("Danger Will Robinson, Danger... Danger!!!\n"); 
      } 
     } 
     else 
     { 
      printf("Danger Will Robinson, Danger... Danger!!!\n"); 
     } 
    */ 

    }//End of while loop 
}//End of Search method 

int main(int argc, char const *argv[]) 
{ 
    //Variables go here: 
    int i = 0; 
    char const *fileName = argv[1]; 


    //Prompt the user: 
    printf("Enter starting fileName: %s\n",argv[1]); 
    //Starting file: 
    printf("Starting from file: %s\n...\n\n\n\n", fileName); 

    //Lettuce(lol) find some files and directories! 
    search(fileName); 


    return 0; 
}//End of main method 
+0

'#include「dirAssignment.h」'對不起,家庭作業需要更多的僞裝。歡迎來到Stack Overflow。請花時間寫一個[**最小,完整**和可驗證示例](/ help/mcve)。 –

+1

我並沒有試圖隱藏它是一項家庭作業的事實。只是想給任何可能有答案的人提供我所有的代碼,因爲也許我寫了一些錯誤導致我的問題。我唯一的問題是爲什麼當我打開if語句時,我得到了打印的內容。我不認爲我是在要求任何人爲我完成一個項目。感謝您的幫助! – tym7953

+0

用於清楚地看到運行時問題的來源,我們希望代碼可以乾淨地編譯。發佈的代碼包含'home grown'頭文件:'dirAssignment.h'但該文件的內容丟失。 – user3629249

回答

0

很明顯你使用了錯誤的變量。 dirInfo由此行初始化if (lstat(fileName, &dirInfo) < 0)。這就是爲什麼總是打印迪爾。

if(S_ISDIR(dirInfo.st_mode)) 
    { 
     printf("Dir\n"); 
    } 
    else if(S_ISREG(dirInfo.st_mode)) 
    { 
     printf("File\n"); 
    } 

在linux上,也許你可以使用dirp->d_type來獲取類型。閱讀this

如果要使用S_ISREG/S_ISDIR,只需調用lstat即可獲取文件狀態。

相關問題