【问题标题】:about filenames in folder [closed]关于文件夹中的文件名[关闭]
【发布时间】:2011-05-11 09:09:21
【问题描述】:

我想获取 c/c++ 文件夹中的所有文件名。我使用 dirent.h,但它在 dirent.h 上显示错误? 我应该如何进行? 有什么办法而不是dirent.h? 提前致谢

【问题讨论】:

  • 操作系统/运行时是什么? faq.cprogramming.com/cgi-bin/…
  • 您需要告诉我们您正在运行什么、遇到了什么错误以及您正在使用的代码。否则我们无能为力
  • 看起来像是编译错误,但我们如何才能确定呢?

标签: c++ filesystems directory amazon-ec2


【解决方案1】:

Boost.Filesystem 可移植地执行此操作。它的tutorial 解释了如何执行这个确切的任务。简而言之:

#include <algorithm>
#include <boost/filesystem.hpp>
#include <iostream>

namespace fs = boost::filesystem;
using namespace std;

int main(int argc, char *argv[])
{
    fs::path p(argv[1]);

    if (fs::exists(p) && fs::is_directory(p))
        copy(fs::directory_iterator(p), fs::directory_iterator(),
             ostream_iterator<fs::directory_entry>(cout, "\n"));

    return 0;
}

如果您确实想使用dirent.h,请将其包含为:

#include <sys/types.h>    /* required before including sys/dirent.h! */
#include <sys/dirent.h>

【讨论】:

    猜你喜欢
    • 2022-01-21
    • 1970-01-01
    • 2016-08-10
    • 2016-08-23
    • 2014-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多