I am iterating through a large set of nested directories searching for files of some extension, say ".foo" using code like the following:
namespace fs = std::filesystem;
int main(int argc, char* argv[])
{
std::ios_base::sync_with_stdio(false);
for (const auto& entry : fs::recursive_directory_iterator("<some directory>")) {
if (entry.path().extension() == ".foo") {
std::cout << entry.path().string() << std::endl;
}
}
}
however the above throws on files with names that use unicode/wide characters. I know I can fix the problem in the little program above by using wstring everywhere i.e. std::wcout << entry.path().wstring() << std::endl;
but what I actually need to do in my real program is skip such files. Right now I am catching the exception in the body of the for-loop and doing nothing in that case but am wondering if there is a more direct approach.
In Windows/Visual Studio the specific exception being thrown is
No mapping for the Unicode character exists in the target multi-byte code page.
How do I test for such filenames using standard C++?
Unicode characters have values > 0x7f
, so you could do something like this:
bool is_wide = false;
for (auto ch : entry.path().wstring())
{
if (ch > 0x7f)
{
is_wide = true;
break;
}
}
if (!is_wide)
std::cout << entry.path().string() << std::endl;