Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get std::string or char[] from filesystem?

Tags:

c++

I have a loop that run through the whole directory I need to get a path from each file in order to use it.

This is my method

#include <experimental/filesystem>

namespace fs = std::experimental::filesystem;

for (const auto & entry : fs::directory_iterator(sourceDir))
{
    if (fs::is_directory(entry))
        continue;
    StreamRAII iStream{ entry, StreamMode::READ_BINARY };
}

issue is that StreamRAII constructor have to get std::string as its first parameter

StreamRAII(const std::string& filename, const char *mode);

But this const auto & entry : fs::directory_iterator(sourceDir), entry is derectory_entry type.

Question is, how to get path to my file? How to convert derectory_entry to std::string or char[]?

P.S. when I use cout I can see this path to my files...

like image 233
Aleksey Timoshchenko Avatar asked Dec 13 '22 10:12

Aleksey Timoshchenko


1 Answers

derectory_entry has a path() function that returns a std::filesystem::path that holds the path name of the file. You can use it's string() function to get a std::string from that path. That would make your code look like

StreamRAII iStream{ entry.path().string(), StreamMode::READ_BINARY };
like image 55
NathanOliver Avatar answered Dec 31 '22 18:12

NathanOliver