我写了一个很简单的 filesystem遍历目录所有文件,并累加文件MB总和。
但是在碰到奇芭的文件名,或文件夹路径时,会报异常,报错。要怎么才能解决?见下图片:
Capture Exception or Error_PrB : 在多字节的目标代码页中,没有此 Unicode 字符可以映射到的字符。
```c++
#include <iostream>
#include <filesystem>
#include <string>
#include <Windows.h>
#include <string.h>
#include <fstream>
#include <string>
namespace fs = std::filesystem;
using namespace std;
double a1_size = 0; //字节大小
//************************************************//
void circlePath(string& str) //
{
try
{
for (auto& i : filesystem::directory_iterator(str))
{
if (fs::is_directory(i.path()))
{
cout << i.path() << endl; //报错
cout << i.path().string() << endl; //报错
string str = i.path().string();
cout << str << endl; //报错
circlePath(str);
}
else
{
cout << i.path().filename() << " < " << i.file_size() << " >" << " bs" << endl;
a1_size += i.file_size(); //显示字节大小
}
}
}
//-------------------------------------------------------------------------------------------------//
catch (exception e)
{
// 异常处理区域
cerr << "Capture Exception or Error_PrB : " << e.what() << endl; //捕获异常,或然后程序结束
}
}
//*************************************************************************************************//
int main()
{
string str("D:\\test");
circlePath(str);
return 0;
}


