C 从原始目录探索子目录

C 从原始目录探索子目录,c,directory,C,Directory,我正在为一个学校项目编写一个“病毒”,从某个目录中删除一个文件。因此,我必须遍历给定的目录以找到指定的文件并将其删除。但是,我可以浏览原始目录,但当我尝试向其传递子目录时,它表示该位置不存在。我也尝试过手动输入位置。同样的事情。这是一个文件夹,里面有一个文件夹。没有别的了 开始文件夹子文件夹1 子文件夹1\u 1 fileToDelete.txt 子文件夹2 子文件夹3 它首先列出子文件夹1、子文件夹2和子文件夹3。我将原始位置与新文件夹连接起来,并递归地传递新位置。我没有这样的文件或目录 S

我正在为一个学校项目编写一个“病毒”,从某个目录中删除一个文件。因此,我必须遍历给定的目录以找到指定的文件并将其删除。但是,我可以浏览原始目录,但当我尝试向其传递子目录时,它表示该位置不存在。我也尝试过手动输入位置。同样的事情。这是一个文件夹,里面有一个文件夹。没有别的了

  • 开始文件夹
    • 子文件夹1
      • 子文件夹1\u 1
        • fileToDelete.txt
  • 子文件夹2
  • 子文件夹3
它首先列出子文件夹1、子文件夹2和子文件夹3。我将原始位置与新文件夹连接起来,并递归地传递新位置。我没有这样的文件或目录

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);

}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
int checkFolder(char*myPath)
{
DIR*d;
结构方向*dir;
int成功=0;
printf(“路径:%s\n”,myPath);
如果((d=opendir(myPath))!=NULL)
{
而((dir=readdir(d))!=NULL)
{
字符*分隔符=“\\”;
char*pathfund=dir->d_name;
char*tempPath=“”;
tempPath=malloc(sizeof(tempPath));
strcpy(tempPath,myPath);
strcat(临时路径、分离器);
strcat(临时路径,路径发现);
//printf(“路径中的文件:%s\n”,tempPath);
if(strstr(tempPath,“.txt”))
{
成功=1;
}
如果(!strchr(tempPath,'.'),则为else
{     
检查文件夹(临时路径);
}
}
closedir(d);
}
其他的
{
perror(“opendir()错误”);
}
回归成功;
}
内部主(空)
{
char*myPath;
成功;
myPath=malloc(sizeof(myPath));
myPath=“StartingFolder”;
成功=检查文件夹(myPath);
printf(“%d\n”,成功);
返回(0);
}

这是一条评论而不是答案,但评论很难编辑

以下是奇怪的情况:

  char * myPath;
  int success;
  myPath = malloc(sizeof(myPath));
  myPath = "StartingFolder";
malloc
在这里毫无意义,是内存泄漏。为指针分配足够的空间,然后立即丢弃该内存,而无需每次释放内存。只需省略
malloc
。另外,通过使用
argv[1]
而不是固定字符串来增加一些灵活性。

原始答案 此问题的常见原因是您没有:

  • 在进行过程中更改目录,或
  • 正确构建完整的路径
readdir()
返回的值是简单的文件名。如果文件位于子目录中,则必须使用子目录和子目录名称作为文件名的前缀。给出的任何一个选项都会起作用。然而,
chdir()
比构建路径更令人担忧——甚至忽略符号链接

评论 虽然“常见原因”通常是故障,但它不是本程序中故障的主要原因

分析 程序中的内存分配不必要地复杂,而且也有错误。这是你的代码的一个变体版本,或多或少根据我的偏见格式化(我还没有调整
uncrustify
中的所有内容以适合我),它可以在我的Mac上运行。它可能(但不是决定性地)也能在Windows上工作-如果您将
“/”
替换为原始代码中的
“\\”
,它肯定会工作

我删除了多余的标题和变量

只有当目录名的名称中不包含
且文件名中包含
时,代码才能正常工作。您的代码将识别文件
hord.txt或二进制文件,因为它包含字符串
.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);
}
#包括
#包括
#包括
#包括
#包括
#包括
静态int checkFolder(char*myPath)
{
DIR*d;
结构方向*dir;
int成功=0;
printf(“路径:%s\n”,myPath);
如果((d=opendir(myPath))!=NULL)
{
而((dir=readdir(d))!=NULL)
{
字符*分隔符=“/”;
char*pathfund=dir->d_name;
字符临时路径[1024];
strcpy(tempPath,myPath);
strcat(临时路径、分隔符);
strcat(临时路径,路径发现);
printf(“路径中的文件:%s\n”,tempPath);
if(strstr(tempPath,“.txt”))
{
成功=1;
printf(“找到:%s\n”,tempPath);
}
else if(strchr(tempPath,'.')==0)
{
检查文件夹(临时路径);
}
}
closedir(d);
}
其他的
{
perror(“opendir()错误”);
}
回归成功;
}
内部主(空)
{
char myPath[]=“StartingFolder”;
int success=checkFolder(myPath);
printf(“%d\n”,成功);
返回(0);
}
应该是

char * seperator = "/";

再加上Jonathan Leffler的回答,你就有了一个从目录中删除文件的可靠“病毒”。

系统调用不是
openfolder
,因为你正在处理的对象不是“文件夹”。它们被称为“目录”。您的
malloc
只分配了4或8个字节。尝试更大的东西,比如
malloc(1024)
。也可以查看一个例子。@William Pursell文件夹和目录本质上是一样的。这要视情况而定。GUI通常表示文件夹,而cmd行/程序通常表示目录。我理解这一点,并且在我的程序中任何地方都不使用openfolder。你能澄清一下吗?“文件夹”是市场部发明的一个词。它在技术讨论中没有一席之地。因为做了其他事情而留下的代码。谢谢你抓住它。我仍然在学习C内存使用的细节,所以有时我倾向于过度查看和/或滥用malloc/free。我不需要为这个p设置动态的位置
char * seperator = "/";