2013-03-06 80 views
0

我在讀取子文件夾中的文件時遇到問題。我在差異文件夾中有大約6000個不同的文件。我會閱讀每個文件。但如果我讀了大約2000個文件,那麼應用程序就不是問題。當我讀取6000個文件意味着整個子文件夾。該應用程序將顯示問題「無法打開文件」。但是,如果我只訪問未打開的文件夾,則應用程序不是問題。我不知道發生了什麼?我想也許我讀了很多文件和內存不夠。你可以編輯幫助我嗎?讀取子文件夾中的許多文件時出錯

//This is code to access subforder 

static int 
find_directory(
     const char *dirname) 
{ 

    DIR *dir; 
    char buffer[PATH_MAX + 2]; 
    char *p = buffer; 
    const char *src; 
    const char* folder_dir; 
    char *end = &buffer[PATH_MAX]; 
    int ok; 

    /* Copy directory name to buffer */ 
    src = dirname; 

    printf("src=%s\n",src); 
    while (p < end && *src != '\0') { 
     *p++ = *src++; 
    } 
    *p = '\0'; 

    /* Open directory stream */ 
    dir = opendir (dirname); 
    if (dir != NULL) { 
     struct dirent *ent; 

     /* Print all files and directories within the directory */ 
     while ((ent = readdir (dir)) != NULL) { 
      char *q = p; 
      char c; 

      /* Get final character of directory name */ 
      if (buffer < q) { 
       c = q[-1]; 
      } else { 
       c = ':'; 
      } 

      /* Append directory separator if not already there */ 
      if (c != ':' && c != '/' && c != '\\') { 
       *q++ = '/'; 
      } 

      /* Append file name */ 
      src = ent->d_name; 
      while (q < end && *src != '\0') { 
       *q++ = *src++; 
      } 
      *q = '\0'; 

      /* Decide what to do with the directory entry */ 
      switch (ent->d_type) { 
       case DT_REG: 
        /* Output file name with directory */ 
        { 
         printf ("FILE=%s\n", buffer); 
         OFBool check= readfile(buffer) 
        } 
        break; 

       case DT_DIR: 
        /* Scan sub-directory recursively */ 
        if (strcmp (ent->d_name, ".") != 0 
          && strcmp (ent->d_name, "..") != 0) { 

         find_directory (buffer,opts); 


        } 
        break; 

       default: 
        /* Do not device entries */ 
        /*NOP*/; 
      } 

     } 

     closedir (dir); 
     ok = 1; 

    } else { 
     /* Could not open directory */ 
     printf ("Cannot open directory %s\n", dirname); 
     ok = 0; 
    } 

    return ok; 
} 
OFBool readfile(const char* filepath) 
{ 
    FILE *f=NULL; 
    OFBool ok = OFFalse; 
    if((f = fopen(filepath, "rb")) == NULL) // checks to see if file exists 
    { 
     ok = OFFalse; 
     cout<<"can not read file"<<filepath<<endl; 
     return ok; 
    } 
    else 
    { 
     ok = true; 
     cout<<" reading OK"<<endl; 
     fclose(f); 
     return ok; 
    } 

} 
+2

這是很多代碼。你可以縮小很多。 – 2013-03-06 13:38:12

+0

你爲什麼要實現自己的'strcpy'和'strcat'? – 2013-03-06 13:48:07

+0

更好的是,這個C++如何? – Angew 2013-03-06 13:49:52

回答

0

怎麼是這樣的:

/* 
* Recursively walk though a directory tree. 
* 
* Arguments: 
*  path - The root directory to start from 
*  hidden - If non-zero, include hidden files and directories 
*/ 
void recurse_directory(const char *path, const int hidden) 
{ 
    DIR *dir = opendir(path); 
    if (dir == NULL) 
    { 
     perror("opendir"); 
     return; 
    } 

    struct dirent *ent; 

    while ((ent = readdir(dir)) != NULL) 
    { 
     if (ent->d_type == DT_DIR) 
     { 
      if (strcmp(ent->d_name, ".") != 0 && 
       strcmp(ent->d_name, "..") != 0 && 
       (ent->d_name[0] != '.' || hidden)) 
      { 
       char *newpath = malloc(strlen(path) + 1 + strlen(ent->d_name) + 1); 
       strcpy(newpath, path); 
       strcat(newpath, "/"); 
       strcat(newpath, ent->d_name); 

       recurse_directory(newpath, hidden); 

       free(newpath); 
      } 
     } 
     else if (ent->d_type == DT_REG) 
     { 
      if (ent->d_name[0] != '.' || hidden) 
      { 
       char *newpath = malloc(strlen(path) + 1 + strlen(ent->d_name) + 1); 
       strcpy(newpath, path); 
       strcat(newpath, "/"); 
       strcat(newpath, ent->d_name); 

       printf("File %s\n", newpath); 
       /* readfile(newpath); */ 

       free(newpath); 
      } 
     } 
    } 
} 
0

太添加到從約阿希姆Pileborg答案。如果您正在處理大量的子目錄,則需要在while循環後添加closedir()調用。這是因爲您一次可以打開的目錄數量有限制。上面的例子在使用小文件夾時工作正常,但使用2000多個子文件夾時,需要在打開另一個文件夾之前關閉目錄。

/* 
* Recursively walk though a directory tree. 
* 
* Arguments: 
*  path - The root directory to start from 
*  hidden - If non-zero, include hidden files and directories 
*/ 
void recurse_directory(const char *path, const int hidden) 
{ 
    DIR *dir = opendir(path); 
    if (dir == NULL) 
{ 
    perror("opendir"); 
    return; 
} 

struct dirent *ent; 

while ((ent = readdir(dir)) != NULL) 
{ 
    if (ent->d_type == DT_DIR) 
    { 
     if (strcmp(ent->d_name, ".") != 0 && 
      strcmp(ent->d_name, "..") != 0 && 
      (ent->d_name[0] != '.' || hidden)) 
     { 
      char *newpath = malloc(strlen(path) + 1 + strlen(ent->d_name) + 1); 
      strcpy(newpath, path); 
      strcat(newpath, "/"); 
      strcat(newpath, ent->d_name); 

      recurse_directory(newpath, hidden); 

      free(newpath); 
     } 
    } 
    else if (ent->d_type == DT_REG) 
    { 
     if (ent->d_name[0] != '.' || hidden) 
     { 
      char *newpath = malloc(strlen(path) + 1 + strlen(ent->d_name) + 1); 
      strcpy(newpath, path); 
      strcat(newpath, "/"); 
      strcat(newpath, ent->d_name); 

      printf("File %s\n", newpath); 
      /* readfile(newpath); */ 

      free(newpath); 
     } 
     } 
    } 
     closedir(dir); /* Add this <--- */ 
} 
相關問題