Home > front end >  how to handle std::system::error exception during recursive_directory_iterator
how to handle std::system::error exception during recursive_directory_iterator

Time:08-09

I was working on a bigger project using the recursive_directory_iterator of std::filesytem, when I stumbled upon this seemingly unknown/unfixable error..

I simplified the project to the bare minimum to recreate the error.. Another questionair found solution in providing skip_permission_denied option, which does nothing for me.

The exception is not limited to this file and happens for random other files too, if I eg delete this one. Though it occurs for the same file every time if i decide not to delete it.

If I use the continue button on VisualStudio three times it is actually able to continue the traversal of files.

What would be a proper way to address this error? And what could potentially cause this?

I removed all exception handling for readability, but the error occurs at the start of the for loop once it reaches this file.

#define NOMINMAX

#include <string>
#include <iostream>
#include <filesystem>
#include <windows.h>
#include <fstream>
#include <winbase.h>
#include <numeric>
#include <string_view>
#include <vector>
#include <shlobj_core.h>
//#include <combaseapi.h>

namespace fs = std::filesystem;

using namespace std;



void startWinXSearch(string argv)
{
    string pathToFolder = argv;

    cout << pathToFolder << endl;

    for (auto& el : std::filesystem::recursive_directory_iterator(pathToFolder, std::filesystem::directory_options::skip_permission_denied)) {
        cout << el << endl;
    }
}

int main(int argc, char* argv[])
{
    CoInitializeEx(NULL, COINIT_MULTITHREADED);

    string test = "C:\\Sciebo";

    startWinXSearch(test);


}

The Exception thrown

CodePudding user response:

Seems to be this error:

ERROR_NO_UNICODE_TRANSLATION

1113 (0x459)

No mapping for the Unicode character exists in the target multi-byte code page.

System Error Codes (1000-1299) (WinError.h) - Win32 apps | Microsoft Docs

Try renaming the file.

CodePudding user response:

Clearly you are getting a non-permission failure on some of the files. So, you will need to either:

  • wrap the failing code in a try..catch, eg:
#define NOMINMAX

#include <string>
#include <iostream>
#include <filesystem>

#include <windows.h>
#include <shlobj_core.h>
//#include <combaseapi.h>

namespace fs = std::filesystem;

void startWinXSearch(const std::string &pathToFolder)
{
    std::cout << pathToFolder << std::endl;

    try {
        for (auto& el : fs::recursive_directory_iterator(pathToFolder, fs::directory_options::skip_permission_denied)) {
            std::cout << el << std::endl;
        }
    }
    catch (const fs::filesystem_error &) {
        // handle error as needed...
    }
}

int main()
{
    CoInitializeEx(NULL, COINIT_MULTITHREADED);

    startWinXSearch("C:\\Sciebo");

    CoUninitialize();
}
  • use recursive_directory_iterator's non-throwing constructor and increment() method (which means you can't use a range-for loop), eg:
#define NOMINMAX

#include <string>
#include <iostream>
#include <filesystem>

#include <windows.h>
#include <shlobj_core.h>
//#include <combaseapi.h>

namespace fs = std::filesystem;

void startWinXSearch(const std::string &pathToFolder)
{
    std::cout << pathToFolder << std::endl;

    std::error_code ec;
    fs::recursive_directory_iterator dir(pathToFolder, fs::directory_options::skip_permission_denied, ec);

    if (ec) {
        // handle error as needed...
    }
    else {
        auto iter = std::begin(dir);
        auto iter_end = std::end(dir);

        while (iter != iter_end) {
            std::cout << *iter << std::endl;
            iter.increment(ec);
            if (ec) {
                // handle error as needed...
            }
        }
    }
}

int main()
{
    CoInitializeEx(NULL, COINIT_MULTITHREADED);

    startWinXSearch("C:\\Sciebo");

    CoUninitialize();
}

Either way, you can then analyze the fs::filesystem_error or std::error_code to find out what kind of OS error is actually occurring.

  • Related