【问题标题】:Reading files in a folder xcode c++读取文件夹中的文件xcode c ++
【发布时间】:2015-10-16 03:21:30
【问题描述】:

我对 C++ 还很陌生。我一直在尝试访问特定文件夹中的文件名。我不确定我应该使用向量还是列表。我尝试了一个列表,只是因为这是我习惯的。

#include <iostream>
#include <string>
#include <fstream>
#include <list>
using namespace std;

list<string> file_list(string file)
{
    list<string> list;
    ifstream files;
    string line;
    files.open(file);
    while (getline(files, line)) {
        list.push_back(line);
    }
    files.close();
    cout << &list << endl;
    return list;
}

当我调试变量 line 时,它保持空白,并打印出 0x7fff5fbff6b0。 我正在尝试访问我的下载文件夹。字符串变量 'file' 是它的直接路径。它打开得很好,但我无法访问其中的文件名。

【问题讨论】:

    标签: c++ xcode file directory


    【解决方案1】:

    C++ 标准库没有文件系统访问库 (yet)。如果你想枚举一个文件夹中的所有文件并且你的编译器没有std::experimental::filesystem,你可以使用boost::filesystem

    【讨论】:

      【解决方案2】:

      这个

      cout << &list << endl;
      

      打印出list的地址,那个神秘的0x7fff5fbff6b0,而不是list的内容。

      要打印内容,您需要更多类似的东西

      for (string & str: list)
      {
          cout << str << endl;
      }
      

      哦,丢了 ;在结束时

      list<string> file_list(string file);
      

      还有杜尔。是的。完全错过了OP试图做的事情。

      像文件一样读取目录不适用于流。您将不得不使用特定于平台的方法。阅读这个问题:How do you iterate through every file/directory recursively in standard C++?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-15
        • 1970-01-01
        • 2011-08-27
        • 2017-03-09
        • 2016-09-21
        相关资源
        最近更新 更多