如何在使用 dirent.h 阅读时跳过目录

how to skip a directory while reading using dirent.h

本文关键字:dirent      更新时间:2023-10-16

我正在尝试使用 dirent.h 中提供的功能递归打开文件

我的问题是:我无法跳过无法打开的目录。我希望它打开它可以打开的目录并跳过它不能打开的目录并移动到下一个目录,而不是失败退出。我应该怎么做才能解决这个问题?

这是我尝试使用的简单代码

int acessdirs(const char *path)
{
    struct dirent *entry;
    DIR *dp;
    char fpath[300];
    if(dp=opendir(path))
    {
        while((entry=readdir(dp)))
            do things here
    }
    else
    {
        std::cout<<"error opening directory";
        return 0;
    }
    return 1;
}

我在windows 7上使用了相同的样式,它工作正常。但是它在windows xp上崩溃了,当我调试它时,我发现它在尝试打开"system volume information"时崩溃了。我真的不需要访问这个文件夹,我希望是否有任何方法可以跳过它。

这是我的真实代码:

它有点长。

int listdir(const char *path) 
{
  struct dirent *entry;
  DIR *dp;
  if(dp = opendir(path))
  {
    struct stat buf ;
    while((entry = readdir(dp)))
    {
        std::string p(path);
        p += "\";
        p += entry->d_name;
         char fpath[300];
        if(!stat(p.c_str(), &buf))
        {
            if(S_ISREG(buf.st_mode))
            {  
                sprintf(fpath,"%s\%s",path,entry->d_name);
                stat(fpath, &buf);
                std::cout<<"n Size of t"<<fpath<<"t"<<buf.st_size;
                fmd5=MDFile (fpath);        
            }//inner second if
            if(S_ISDIR(buf.st_mode) &&  
         // the following is to ensure we do not dive into directories "." and ".."
                      strcmp(entry->d_name, ".")  && strcmp(entry->d_name, "..") )
            {
                listdir(p.c_str());
            }
        }//inner first if
        else
            std::cout << "ERROR in statn";
    }//end while
    closedir(dp);
  }//first if
  else
  {
      std::cout << "ERROR in opendirn";
      return 0;
  }
  return 1;   
 }//listdir()

你最大的问题似乎在这里:

sprintf(fpath,"%s\%s",path,entry->d_name);
stat(fpath, &buf);

没有看到fpath的衰落,很难确定,但你要么

  • sprintf调用中的溢出fpath,导致未定义的行为。"系统卷信息"是一个很长的名称。你真的应该使用snprintf.

  • 不检查stat调用的返回值。如果它返回-1,我不确定buf的内容是什么。

更重要的是,如果你可以使用POSIX的东西,那么功能ftw是标准的,并且应该提供你在这里尝试实现的大部分功能。