【问题标题】:How to get all filename in a given directory如何获取给定目录中的所有文件名
【发布时间】:2009-11-12 13:25:39
【问题描述】:

我想获取给定路径中的文件名,是否有可用的 api。我的编程环境是vc++ mfc

【问题讨论】:

    标签: visual-studio-2008 visual-c++ mfc


    【解决方案1】:

    您应该查看FindFirstFileFindNextFile,或MFC 的包装器CFileFind

    【讨论】:

      【解决方案2】:

      Boost 有一个很好的独立于平台的filesystem library。它将与 MFC 一起使用。

      这是来自their reference 的示例:

      #include <iostream>
      #include <filesystem>
      
      using std::tr2::sys;
      using std::cout;
      
      int main(int argc, char* argv[])
      {
        std::string p(argc <= 1 ? "." : argv[1]);
      
        if (is_directory(p))
        {
          for (directory_iterator itr(p); itr!=directory_iterator(); ++itr)
          {
            cout << itr->path().filename() << ' '; // display filename only
            if (is_regular_file(itr->status())) cout << " [" << file_size(itr->path()) << ']';
            cout << '\n';
          }
        }
        else cout << (exists(p) ? "Found: " : "Not found: ") << p << '\n';
      
        return 0;
      }
      

      【讨论】:

        【解决方案3】:

        您也可以使用 MFC:CFileFind

        【讨论】:

          【解决方案4】:

          还有CListBox::Dir。如果您想用文件名填充列表框,这非常方便。

          【讨论】:

            猜你喜欢
            • 2016-03-07
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-08-11
            • 1970-01-01
            • 2019-11-03
            相关资源
            最近更新 更多