How to get the names of the files in a directory? C ++

3

I have a media / map directory, in that folder there are many files, example ".mesh.xml", ".mesh", etc. I need to create a function that stores the names of the ".mesh" files in a string array and ignores the others including ".mesh.xml".

Clarification: the array should keep the name + the format, that is, example.mesh

Why do I want this? To save me the job of loading them manually.

What do I have until now? Something semi-automatic, I simply have an array of string where I manually put the names of the files (currently they are 58, but later the figure will exceed 100), and then this code:

  Ogre::SceneNode *auxNode;
  Ogre::Entity *auxEnt;

  for(int i = 0; i<57; i++){
      string nameNode = "SG_Map";
      nameNode+=static_cast<std::ostringstream*>(&(std::ostringstream() << i))->str();
      auxNode = _sceneMgr->createSceneNode(nameNode);
      auxEnt = _sceneMgr->createEntity(myArray[i]);
      auxNode->attachObject(auxEnt);
      _sceneMgr->getRootSceneNode()->addChild(auxNode);
  }
    
asked by Strelok 30.05.2017 в 00:10
source

1 answer

3

Access to the file system is platform dependent, so without knowing the operating system you work with, you only have to use a generic library like Boost Filesystem , in its tutorial have an example of how to navigate the elements of a folder:

Boost
#include <boost/filesystem.hpp>

void muestra_contenido_de(const std::string &a_carpeta)
{
    boost::filesystem::path p(a_carpeta);

    if (boost::filesystem::exists(p) && boost::filesystem::is_directory(p))
    {
        std::cout << p << " contiene:\n";

        for (auto &x : boost::filesystem::directory_iterator(p))
              cout << "    " << x.path() << '\n'; 
    }
}

If you can not or do not want to use Boost, your code should be platform dependent, in Windows:

Windows
#include <windows.h>

void muestra_contenido_de(const std::string &a_carpeta)
{
    WIN32_FIND_DATA ffd{};

    if (auto handle = FindFirstFile((a_carpeta + "/*.*").c_str(), &ffd))
    {
        do
        {
            std::cout << ffd.cFileName << '\n';
        }
        while (FindNextFile(handle, &ffd) != 0);
    }
}

And here for Linux:

Linux
#include <dirent.h>

void muestra_contenido_de(const std::string &a_carpeta)
{
    if (DIR *dpdf = opendir(a_carpeta.c_str()))
    {
        dirent *epdf = nullptr;
        while (epdf = readdir(dpdf))
        {
            std::cout << epdf->d_name << '\n';
        }
    }
}

As a final note, the approval of the C ++ 17 standard is foreseen for this August, this standard is expected to include a standard file system access library, you can read the technical documents of this proposal here:

  • P0218R0 Adopt the file system technical specification for C ++ 17.
  • P0219R1 Relative Routes for File System (First revision).
  • P0317R1 Cache for directory_entry (First revision).
  • P0392R0 Filesystem paths must adapt string_view .
  • P0430R2 File system library on non-POSIX operating systems.
  • P0492R2 Proposed resolutions to the comments of C ++ 17 National Body on File System (Second revision).

This new standard library will allow the following:

C++17
#include <filesystem>

void muestra_contenido_de(const std::string &a_carpeta)
{
    for (auto &p : std::filesystem::directory_iterator(a_carpeta))
        std::cout << p << '\n';
}
    
answered by 30.05.2017 / 10:21
source