Disk Partitions Information:
Drive: C:\ (Local Drive) - Total: 119 GB
Drive: D:\ (Local Drive) - Total: 341 GB
Drive: G:\ (Local Drive) - Total: 15 GB
Pick a path: C:\
"C:\\$Recycle.Bin"
"C:\\$Recycle.Bin\\S-1-5-18"
Error: Unable to iterate through the path C:\ (filesystem error: cannot increment recursive directory iterator: Invalid argument)
Permissions for "C:\\":
rwxrwxrwx
terminate called after throwing an instance of 'std::filesystem::__cxx11::filesystem_error'
what(): filesystem error: cannot set permissions: Permission denied [C:\]
对于 D,我只得到错误“无法迭代路径 C:\(文件系统错误:无法递增递归目录迭代器:参数无效)”
我的目标是打印目录中的所有文件,当我遇到此错误时,我认为这是有关权限或其他什么的问题,所以我尝试强制所有权限,因为这就是我以前在Python中所做的,但是我觉得在 C++ 中它不起作用。
void check_permissions(const fs::path& path) {
fs::perms p = fs::status(path).permissions();
std::cout << "Permissions for " << path << ":\n";
std::cout << ((p & fs::perms::owner_read) != fs::perms::none ? "r" : "-")
<< ((p & fs::perms::owner_write) != fs::perms::none ? "w" : "-")
<< ((p & fs::perms::owner_exec) != fs::perms::none ? "x" : "-")
<< ((p & fs::perms::group_read) != fs::perms::none ? "r" : "-")
<< ((p & fs::perms::group_write) != fs::perms::none ? "w" : "-")
<< ((p & fs::perms::group_exec) != fs::perms::none ? "x" : "-")
<< ((p & fs::perms::others_read) != fs::perms::none ? "r" : "-")
<< ((p & fs::perms::others_write) != fs::perms::none ? "w" : "-")
<< ((p & fs::perms::others_exec) != fs::perms::none ? "x" : "-")
<< '\n';
}
void set_permissions(const fs::path& path, fs::perms permissions) {
fs::permissions(path, permissions);
}
下面是打印所有文件的函数:
void list_files_recursively(const std::string& path) {
try {
for (fs::directory_entry entry : fs::recursive_directory_iterator(path)) {
try {
std::cout << entry.path() << std::endl;
} catch (const fs::filesystem_error& e) {
std::cerr << "Error accessing: " << entry.path() << " (" << e.what() << ")\n";
}
}
} catch (const fs::filesystem_error& e) {
std::cerr << "Error: Unable to iterate through the path " << path << " (" << e.what() << ")\n";
}
}
使用Google驱动器G:,它可以正常工作,没有任何问题,我也尝试跳过我无权访问的文件,但不起作用。
最小可重现示例
#include <iostream>
#include <filesystem>
#include <windows.h>
namespace fs = std::filesystem;
void print_files(const std::string& path) {
try {
for (const auto& entry : fs::recursive_directory_iterator(path, fs::directory_options::skip_permission_denied)) {
try {
std::cout << entry.path() << std::endl;
} catch (const fs::filesystem_error& e) {
std::cerr << "Error accessing: " << entry.path() << " (" << e.what() << ")\n";
}
}
} catch (const fs::filesystem_error& e) {
std::cerr << "Error: Unable to iterate through the path " << path << " (" << e.what() << ")\n";
}
}
int main() {
std::string path;
std::cout << "Pick a path: ";
std::cin >> path;
print_files(path);
return 0;
}
它返回相同的错误。
我在另一个 StackOverflow 页面上写了这个。它不适用于
C:\\
,但适用于特定文件夹,但不适用于 C:\\
。
void print_files(const std::string& path) {
try {
for (const auto& entry : fs::recursive_directory_iterator(path, fs::directory_options::skip_permission_denied)) {
try {
// Skip system and hidden files
if ((entry.status().permissions() & fs::perms::others_read) == fs::perms::none) {
continue;
}
std::cout << "Path: " << entry.path()
<< " | Type: " << (entry.is_directory() ? "Directory" : "File")
<< " | Size: " << (entry.is_regular_file() ? std::to_string(entry.file_size()) + " bytes" : "N/A")
<< std::endl;
} catch (const fs::filesystem_error& e) {
std::cerr << "Error accessing: " << entry.path() << " (" << e.what() << ")\n";
}
}
} catch (const fs::filesystem_error& e) {
std::cerr << "Error: Unable to iterate through the path " << path << " (" << e.what() << ")\n";
}
}
int main() {
std::string path;
std::cout << "Pick a path: ";
std::cin >> path;
if (!fs::exists(path)) {
std::cerr << "Error: Path does not exist.\n";
return 1;
}
print_files(path);
return 0;
}
// BUT THIS STILL DOESN'T WORK WITH C:\
正如 @PaulMcKenzie 所说,通过以管理员身份运行 VsCode 可以轻松避免此错误
要以管理员身份运行它,您可以双击 VsCode 并向下滚动选项,直到找到 以管理员身份运行
或者随时以管理员身份运行它
每次打开应用程序时都会以管理员身份运行