C++ - Read the name of a directory’s files

Asked

Viewed 376 times

0

I am making a program to read all files from a folder, and clear the name of them ( take any accent ).

I wonder if there is a standard library to work with directories(Read). I am using Windows 10, Visual Code with Mingw.

The only example that partially met me is:

#include <iostream>
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>

int main (void)
{
  DIR *dp;
  struct dirent *ep;

  dp = opendir ("./");
  if (dp != NULL)
    {
      while (ep = readdir (dp))
        puts (ep->d_name);
      (void) closedir (dp);
    }
  else{
    perror ("Couldn't open the directory");
  }  
  system("pause");
  return 0;
}

1 answer

1

You can use the library Boost the component filesystem example:

#include <iostream>
#include <boost/filesystem.hpp>
using namespace boost::filesystem;

int main(int argc, char* argv[])
{
   path Desktop("C://Desktop");
   recursive_directory_iterator end;

   for(recursive_directory_iterator i(Desktop); i != end; ++i)
   {
       path file_name = *i;
       std::cout << file_name.string() << std::endl;
   }
   return 0;
}

https://www.boost.org/doc/libs/1_71_0/libs/filesystem/doc/index.htm

Browser other questions tagged

You are not signed in. Login or sign up in order to post.