如何在标准C ++中递归地遍历每个文件/目录? [英] How do you iterate through every file/directory recursively in standard C++?

查看:179
本文介绍了如何在标准C ++中递归地遍历每个文件/目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在标准C ++中以递归方式遍历每个文件/目录?

How do you iterate through every file/directory recursively in standard C++?

推荐答案

在标准C ++中,方式这样做,因为标准C ++没有目录的概念。如果您想扩展网络一点,您可能需要查看 Boost.FileSystem 。这已经被接受包含在TR2中,所以这给你最好的机会,让你的实现尽可能接近标准。

In standard C++, technically there is no way to do this since standard C++ has no conception of directories. If you want to expand your net a little bit, you might like to look at using Boost.FileSystem. This has been accepted for inclusion in TR2, so this gives you the best chance of keeping your implementation as close as possible to the standard.

一个例子,直接从网站:

An example, taken straight from the website:

bool find_file( const path & dir_path,         // in this directory,
                const std::string & file_name, // search for this name,
                path & path_found )            // placing path here if found
{
  if ( !exists( dir_path ) ) return false;
  directory_iterator end_itr; // default construction yields past-the-end
  for ( directory_iterator itr( dir_path );
        itr != end_itr;
        ++itr )
  {
    if ( is_directory(itr->status()) )
    {
      if ( find_file( itr->path(), file_name, path_found ) ) return true;
    }
    else if ( itr->leaf() == file_name ) // see below
    {
      path_found = itr->path();
      return true;
    }
  }
  return false;
}

这篇关于如何在标准C ++中递归地遍历每个文件/目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆