2011-11-16 59 views
2

我試圖創建一個函數來掃描我的Windows PC上的一個文件夾,並且每次它執行時,一個名爲「Filter.txt」的文件附加了字符串「Test Script」 。掃描一個目錄並修改文件c

現在的問題是2,首先是掃描必須在目錄c:\ LOG或其子目錄中執行,其次是我不知道如何鏈接目錄中的fopen和名稱的文件。

int main(){ 
    DIR *dir; 
    FILE * pFile; 
    char myString[100]; 
    struct dirent *ent; 
    dir = opendir ("c:\\LOG"); 
    if (dir != NULL) { 
     /* print all the files and directories */ 
     while ((ent = readdir (dir)) != NULL) { 
      pFile = fopen ("Filter.txt","a"); 
      if (pFile==NULL) 
       perror("Error"); 
      else 
       fprintf(pFile,"%s\n","Test scriptIno"); 
      fclose(pFile); 
      //printf ("%s\n", ent->d_name); 
     } 
     closedir (dir); 
    } else { 
     /* Can not open directory */ 
     perror (""); 
     return EXIT_FAILURE; 
    } 
} 
+0

您不應該將C用於這樣的任務。更好地觀察像Perl這樣的腳本語言。每種語言/技術都是爲某種目的而開發的。 C被開發用來製作操作系統,而不是做腳本。如果您有興趣,我可以在Perl中向您展示一個示例。 – m0skit0

+2

@ m0skit0:在K&R中,他們將C描述爲「通用語言」。我沒有找到像「僅用於開發操作系統」這樣的聲明。 –

+0

@undur_gongor:那是在1972年。現在你不想用C編寫這樣的腳本。這只是一個浪費時間,除非你想要真正學會C.這就是說,C可能是我最喜歡的語言,但是人們必須知道什麼時候該用什麼。 – m0skit0

回答

1

有關可以如何鏈opendir調用這裏SO找到很多答案,例如this。使用ent->d_type檢查條目是否是目錄或文件。

要打開目錄中的文件,只需使用ent->d_name中的路徑名來構造fopen調用的路徑。

編輯在工作有些無聊,搞得像你也許想要的功能......

#ifdef _WIN32 
# define DIR_SEPARATOR "\\" 
#else 
# define DIR_SEPARATOR "/" 
#endif 
void my_readdir(const char *path) 
{ 
    DIR *dir = opendir(path); 
    if (dir != NULL) 
    { 
     struct dirent *ent; 

     static const char filtername[] = "filter.txt"; 

     /* +2: One for directory separator, one for string terminator */ 
     char *filename = (char *) malloc(strlen(path) + strlen(filtername) + 2); 

     strcpy(filename, path); 
     strcat(filename, DIR_SEPARATOR); 
     strcat(filename, filtername); 

     FILE *fp = fopen(filename, "a"); 

     while ((ent = readdir(dir)) != NULL) 
     { 
      if (ent->d_type == DT_REG || ent->d_type == DT_DIR) 
      { 
       if (strcmp(ent->d_name, "..") != 0 && strcmp(ent->d_name, ".") != 0) 
       { 
        if (fp != NULL) 
         fprintf(fp, "%s : %s\n", (ent->d_type == DT_REG ? "File" : "Directory"), ent->d_name); 

        if (ent->d_type == DT_DIR) 
        { 
         /* +2: One for directory separator, one for string terminator */ 
         char *newpath = (char *) malloc(strlen(path) + strlen(ent->d_name) + 2); 

         strcpy(newpath, path); 
         strcat(newpath, DIR_SEPARATOR); 
         strcat(newpath, ent->d_name); 

         /* Call myself recusively */ 
         my_readdir(newpath); 

         free(newpath); 
        } 
       } 
      } 
     } 

     if (fp != NULL) 
      fclose(fp); 
     free(filename); 
    } 
} 

編輯看來,opendirreaddir功能不是很好的支持在Windows上。以與上述示例類似的方式使用僅Windows的FindFirstFileFindNextFile。有關如何使用這些功能的示例,請參見this MSDN page

+0

如果我創建這樣的路徑myString = strcat(ent-> d_type,「\\ Filter.txt」);編譯器響應「結構中沒有名爲'd_type'的成員」和「賦值中的不兼容類型」 – AleMal

+0

@ user1035523'ent-> d_type'是文件的類型,而不是名稱。您必須動態創建一個新字符串以包含路徑的所有單獨部分。 –

+0

非常感謝,但是當我運行函數編譯器響應「'DT_REG'未聲明(首次在此函數中使用)」在行「if(ent-> d_type == DT_REG || ent-> d_type == DT_DIR)」 – AleMal