如何将boost :: filesystem :: directory_iterator转换为const char*

nik*_*hil 12 c++ boost boost-filesystem

我想遍历目录中的所有文件并打印其内容.Boost非常好地处理迭代部分,但我不知道如何将其转换为const char *.

boost::filesystem::directory_iterator path_it(path);
    boost::filesystem::directory_iterator end_it;
    while(path_it != end_it){
      std::cout << *path_it << std::endl;

      // Convert this to a c_string
      std::ifstream infile(*path_it);
    }
Run Code Online (Sandbox Code Playgroud)

我试着阅读这个文档,但找不到像string或的东西c_str().我是两个人的新手C++,boost并希望找到一些javadoc类似的文档,基本上可以告诉我成员是什么以及可用的功能而不是转储源代码.

对不起咆哮,但有人可以告诉我如何转换*path_itc string.

Pet*_*ood 24

当您取消引用迭代器时,它返回一个directory_entry:

const directory_entry& entry = *path_it;
Run Code Online (Sandbox Code Playgroud)

你可以使用它,operator<<并且ostream正如你所发现的那样:

std::cout << entry << std::endl;
Run Code Online (Sandbox Code Playgroud)

您可以使用ostringstream以下方法创建字符串

std::ostringstream oss;

oss << entry;

std::string path = oss.str();
Run Code Online (Sandbox Code Playgroud)

或者,您可以string直接从directory_entry以下位置访问路径:

std::string path = entry.path().string();
Run Code Online (Sandbox Code Playgroud)