[英]c++ 17 filesystem copy_file access denied
I'm using visual studio 2017, running with the c++17 ISO Standard(not boost) set to be able to use <filesystem>
.我正在使用visual studio 2017,运行c++17 ISO标准(不是boost)设置为能够使用
<filesystem>
。 I'm running into a wall though because everytime I run, whether in debug or release, file_copy()
gives me the error access denied.不过,我遇到了一堵墙,因为每次我运行时,无论是在调试还是发布中,
file_copy()
都会给我错误访问被拒绝。 I've checked the other bits of my code and the only thing that isn't working is file_copy()
.我检查了我的代码的其他部分,唯一不起作用的是
file_copy()
。 Does anyone know why I'm getting this error and how to fix it?有谁知道我为什么会收到这个错误以及如何解决它? I'm the administrative account on my PC.
我是我 PC 上的管理帐户。
std::vector<std::string> findAndCopyFiles()
{
std::vector<std::string> fileNames;
std::error_code errCode;
errCode.clear();
fs::current_path("C:\\Users\\kenny\\Desktop\\Engine", errCode);
std::cout << errCode.message() << std::endl; errCode.clear();
fs::path pa = fs::current_path();
pa += "\\TEMP";
std::cout << pa.string() << std::endl;
if (fs::create_directory(pa, errCode))//Create directory for copying all files)
{
std::cout << "Directory created successfully" << std::endl;
std::cout << errCode.message() << std::endl; errCode.clear();
}
fs::path tempDir(pa);
fs::path currentDirectory = fs::current_path();
fs::recursive_directory_iterator dirIter(currentDirectory);
for (auto &p : dirIter)
{
if (p.path().extension() == ".cpp" || p.path().extension() == ".h")
{
//std::string fileContents = getFileContents(p.path().string());
std::string fileName = p.path().stem().string();
if (!fs::copy_file(p.path(), tempDir, fs::copy_options::overwrite_existing, errCode))
{
std::cout << "failed to copy file: " << fileName << " from " << p.path().string() << " to " << tempDir.string() <<std::endl;
}
std::cout << errCode.message() << std::endl; errCode.clear();
//ensures file is a cpp file before adding it to list of fileNames
if (p.path().extension().string() == ".cpp")
{
auto it = std::find(fileNames.begin(), fileNames.end(), fileName); //seaches TEMP folder for file
if (it == fileNames.end())
{//if file was not found in vector of registered file names, add it
fileNames.push_back(fileName);
}
}
}
}
std::cout << "All files found. " << fileNames.size() << " files were found" << std::endl;
return fileNames;
}
As per the comments.根据评论。 You were trying to overwrite a directory with a regular file.
您试图用常规文件覆盖目录。 From the documentation [trimmed]
从文档[修剪]
o Otherwise, if the destination file already exists...
o Report an error if any of the following is true:
o to and from are the same as determined by equivalent(from, to);
o to is not a regular file as determined by !is_regular_file(to)
So you need to append the filename to the destination directory path using the `std::filesystem::operator/ overload (untested)...因此,您需要使用`std::filesystem::operator/重载(未经测试)将文件名附加到目标目录路径...
if (!fs::copy_file(p.path(), tempDir / p.filename(), fs::copy_options::overwrite_existing, errCode))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.