簡體   English   中英

從原始目錄瀏覽子目錄

[英]Exploring sub-directory from original directory

我正在為學校項目編寫“病毒”,以從某個目錄中刪除文件。 因此,我必須遍歷給定目錄以找到指定的文件並將其刪除。 但是,我可以瀏覽原始目錄,但是當我嘗試將其傳遞給子目錄時,它說該位置不存在。 我也嘗試過手動放置位置。 一樣。 這是一個文件夾,里面有一個文件夾。 沒有其他的

  • StartingFolder
    • 子文件夾1
      • SubFolder1_1
        • fileToDelete.txt
  • 子文件夾2
  • 子文件夾3

它開始列出SubFolder1,SubFolder2和SubFolder3。 我將原始位置與新文件夾連接起來,然后遞歸傳遞新位置。 我沒有得到這樣的文件或目錄。

StartingFolder是與正在運行的程序位於同一目錄的文件夾。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>
#include <assert.h>

int checkFolder(char * myPath)
{
DIR           *d;
struct dirent *dir;

int success = 0;
printf("Path: %s\n", myPath);
if ((d = opendir(myPath)) != NULL)
{

   while ((dir = readdir(d)) != NULL)
  {

     char * seperator = "\\";
     char * pathFound = dir->d_name;   
     char * tempPath = "";         

     tempPath = malloc(sizeof(tempPath));
     strcpy(tempPath, myPath);

     strcat(tempPath, seperator);
     strcat(tempPath, pathFound);
      //printf("Files in Path: %s\n",tempPath);

      if(strstr(tempPath, ".txt"))
      {
         success = 1;
      }
      else if(!strchr(tempPath, '.'))
      {     
         checkFolder(tempPath);
      }



  }

  closedir(d);
}
 else
  {
     perror("opendir() error");
 }

return success;
}

 int main(void)
 {

char * myPath;
int success;
myPath = malloc(sizeof(myPath));
myPath = "StartingFolder";
success = checkFolder(myPath);
printf("%d\n",success);

return(0);

}

原始答案

造成此問題的通常原因是您沒有:

  • 在進行過程中更改目錄, 或者
  • 正確構建完整路徑。

readdir()返回的值是簡單的文件名。 如果在子目錄中,則必須在文件名前添加子目錄和子目錄名。 給出的任何一個選項都可以使用。 但是, chdir()比構建路徑更麻煩-甚至忽略符號鏈接。

評論

盡管“常見原因”通常是麻煩,但它不是此程序中主要的麻煩原因。

分析

程序中的內存分配不必要地復雜,並且也是錯誤的。 這是您的代碼的變體版本,根據我的偏見或多或少進行了格式化(我尚未對所有內容進行uncrustify以適應我的需求),該版本適用於Mac。 它也可能(但不是確定地)在Windows上也可以工作-如果將"/"替換為原始代碼中的"\\\\" ,則肯定可以工作。

我刪除了多余的標題和變量。

僅當目錄名不包含時,您的代碼才能正常運行. 其名稱和文件中確實包含一個. 在名字里。 您的代碼將識別文件horrid.txt-or-binary因為它包含字符串.txt

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

static int checkFolder(char *myPath)
{
    DIR           *d;
    struct dirent *dir;

    int success = 0;
    printf("Path: %s\n", myPath);
    if ((d = opendir(myPath)) != NULL)
    {
        while ((dir = readdir(d)) != NULL)
        {
            char *separator = "/";
            char *pathFound = dir->d_name;
            char  tempPath[1024];

            strcpy(tempPath, myPath);
            strcat(tempPath, separator);
            strcat(tempPath, pathFound);
            printf("Files in Path: %s\n", tempPath);

            if (strstr(tempPath, ".txt"))
            {
                success = 1;
                printf("Found: %s\n", tempPath);
            }
            else if (strchr(tempPath, '.') == 0)
            {
                checkFolder(tempPath);
            }
        }

        closedir(d);
    }
    else
    {
        perror("opendir() error");
    }

    return success;
}

int main(void)
{
    char myPath[] = "StartingFolder";
    int success = checkFolder(myPath);
    printf("%d\n", success);
    return(0);
}

這是評論而不是答案,但是評論很難編輯。

以下是奇怪的:

  char * myPath;
  int success;
  myPath = malloc(sizeof(myPath));
  myPath = "StartingFolder";

malloc在這里完全沒有意義,並且是內存泄漏。 您為指針分配了足夠的空間,然后立即丟棄該內存,而無需每次釋放它。 只需省略malloc 另外,通過使用argv[1]而不是固定字符串來增加靈活性。

char * seperator = "\\";

應該

char * seperator = "/";

結合使用Jonathan Leffler的答案,您將獲得可靠的“病毒”,可以從目錄中刪除文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM