std::filesystem::directory_iterator fails with cyrillic letters

363 Views Asked by At

I am trying to create a file retriever. It works fine so far, except for the fact that it quits if e.g. Cyrillic characters appear in the file or directory name. In the C++ description of directory_iterator, I didn't find any hints about this. I am puzzled by this, especially since the Windows OS has no difficulties with Cyrillic characters, and I did not find any hints in the corresponding cppreference docs. What's the issue? Which keywords do I have to deal with?

/** C++17 boost-less fileSubDirIteration, core from
https://www.cppstories.com/2019/04/dir-iterate/  */

/** mandatory */
#include <filesystem>
#include <fstream>
#include <stdio.h>
namespace fs = std::filesystem;
std::string dirList[100];
std::ofstream fp;
void DisplayPath(const fs::path&, int); 
  
int main(int argc,char* argv[])
{
    if (argc < 3) { printf("fSDI.exe <startPath> <outFile>\n"); exit(0); }
    const fs::path toRetrieve(argv[1]);
    std::string out(argv[2]);
    fp.open(out, std::ios::out);
    fp << "retrieve: " << toRetrieve << std::endl;
    dirList[0] = fs::absolute(toRetrieve).string();
    DisplayPath(toRetrieve,0);
    fp.close();
}

void DisplayPath(const fs::path& pathToScan,int lv = 0)
{
    for (const auto& entry : fs::directory_iterator(pathToScan))
    {
        const auto file = entry.path().filename().string();
        if (entry.is_directory())
        {
            fp << "[" <<  entry.path().filename().string() << "]" << std::endl;
            dirList[lv+1] = entry.path().filename().string();
            DisplayPath(entry, lv + 1);
        }
        else if (entry.is_regular_file())
        {
            std::string fullPath;
            fullPath += dirList[0];
            if (!(dirList[0].compare(2, 1, "\\", 0, 1) == 0 && dirList[0].size() == 3))
                fullPath += "\\";
            for(int i=1;i<=lv;i++) fullPath += (dirList[i] + "\\" );
            fp << " " << fullPath << file << std::endl;
        }
        else;
    }
}
0

There are 0 best solutions below