【问题标题】:How to group data while iterating through a directory in c++如何在 C++ 中遍历目录时对数据进行分组
【发布时间】:2016-04-03 20:54:01
【问题描述】:

我有一个包含 15 个文件夹的目录,每个文件夹有 100 个文本文件。在每个文本文件中都包含一列数字。

我需要这些数字来做一些计算,但我不知道如何获得它。我在考虑一个二维向量,但我需要不同类型的数据结构(文件夹名称的字符串和数字的整数)。

我最好的解决方案是什么?d

到目前为止,我得到的是一个代码,它将通过给定的路径搜索所有文件。

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <algorithm>
#include <tuple>
#include <boost/filesystem.hpp>
#include<dirent.h>

using namespace std;

namespace fs = boost::filesyst

// prototype to search all the files by given it a path
vector<double> getFilesFromDirectory(const fs::path& startDirectory);

int main()
{   // the directory
    string dir =  "/home/...";

    // testing to call my methode
    vector<double> myDataStructure = getFilesFromDirectory(dir);

    // print out the value of myDataStructure
    for (auto it = myDataStructure.begin(); it != myDataStructure.end(); it++)
    {
        cout << *it << " " << endl;
    }

    return 0;
}

// methode to search all the files by given it a path
vector<double> getFilesFromDirectory(const fs::path& startDirectory) 
{
    vector<double> di; 

    // First check if the start path exists
    if (!fs::exists(startDirectory) || !fs::is_directory(startDirectory))
    {
        cout << "Given path not a directory or does not exist" << endl;
        exit(1);
    }

    // Create iterators for iterating all entries in the directory
    fs::recursive_directory_iterator it(startDirectory); // Directory iterator at the start of the directory
    fs::recursive_directory_iterator end; // Directory iterator by default at the end

    // Iterate all entries in the directory and sub directories
    while (it != end)
    {
        // Print leading spaces
        for (int i = 0; i < it.level(); i++)
            cout << "";

        // Check if the directory entry is an directory
        // When directory, print directory name.
        // Else print just the file name.
        if (fs::is_directory(it->status()))
        {
            // print out the path file
            cout << it->path() << endl; 
        }
        else
        { 
            cout << it->path().filename() << endl;

            // test
            di = getValueFromFile(it->path().c_str());

            // test, here I want to group the numbers of the file
            // and each name of the folder
            for(int i = 0; i < 15; i++)
            {
                di.push_back(mi(fs::basename(it->path()), it->path().c_str());
            }
        }

        // When a symbolic link, don't iterate it. Can cause infinite loop.
        if (fs::is_symlink(it->status()))
            it.no_push();

        // Next directory entry
        it++;
    }  
    return di;
}

【问题讨论】:

  • 文件是否以某种方式相互关联,或者是独立的?这是一种转变,还是某种减少?如果它是您感兴趣的每个文件的单个值,并且您为每个文件报告这些结果,为什么不直接使用 std::vector&lt;std::tuple&lt;boost::filesystem::path, ValueType&gt;&gt; 其中 ValueType 是您在计算中使用的任何类型?
  • 文件与文件夹相互关联。我的想法是在获得每个文件夹的每个数字后在 Excel 中制作图表。嗯,也许这可以工作。我会看看。谢谢
  • 是的,很抱歉我没有显示所有代码。此代码将文本文件转换为双精度文件。 getValueFromFile 方法就是这样做的。

标签: c++ data-structures directory iteration boost-filesystem


【解决方案1】:

如果我正确理解了问题,我会编写一个类(或结构)来保存每个文件的内容:

包含路径的字符串: 包含该文件列中表示的每个值的向量

在您的主程序中,包含您创建的每个对象的向量。

定义:

#ifndef __COLVALS_HPP__
#define __COLVALS_HPP__

#include <vector>
#include <string>

class ColVals {

private:
  std::vector<double> _colValues;
  std::string         _pathName;

public:
  ColVals(const std::string& pathName);
  ~ColVals() {}

  void appendValue(const double colValue);

  std::vector<double> getValues();

  std::string getPath();
};

#endif // __COLVALS_HPP__

实施:

#include "colvals.hpp"

using namespace std;

ColVals::ColVals(const string& pathName) {
  _pathName = pathName;
}

void ColVals::appendValue(const double colValue) {
  _colValues.push_back(colValue);
}

vector<double> ColVals::getValues() {
  return _colValues;
}

string ColVals::getPath() {
  return _pathName;
}

【讨论】:

  • 你能举个例子吗?
  • 谢谢你的例子,但这是否仍然意味着我需要使用 2D 矢量来创建 15 个对象:即 15 个文件夹。或者只是一个循环将创建 15 个对象,每个对象都包含文本文件的值。
  • 只有一个 ColVals 类型的向量,内部 _colValues 保留另一个维度,由您的列值组成。
  • 你能用一个例子解释一下,我无法掌握 c++ 的窍门。还没有;)
  • 先尝试一下。如果你想学习,它会更有效。对于要从​​中获取值的每个文件,使用目录名称创建此 ColVals 对象。现在遍历文件值并使用对象成员函数“appendValue”将每个值插入到对象中。从文件中插入最后一个值后,将对象附加到 ColVals 类型的向量。
猜你喜欢
  • 1970-01-01
  • 2019-06-08
  • 2010-11-19
  • 2021-01-19
  • 2021-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-13
相关资源
最近更新 更多