移动文件() 不起作用
MoveFile() doesn't work
我正试图将一些文件从一个目录移动到同一驱动器上的另一个目录。所有目标目录都存在,但MoveFile()
不起作用,GetLastError()
返回0。我是c++的新手,我不知道是什么导致了这个问题。下面是代码:
try
{
destinationFilePath = i->FilePath;
destinationFilePath=destinationFilePath.substr(destinationFilePath.find(GENERAL_SAVE_PATH) + strlen(GENERAL_SAVE_PATH.c_str()));
destinationFilePath = Backup_Save_Path + destinationFilePath;
vector<string> folders;
StringSplit(destinationFilePath,"\",folders);
string pathToCreate;
string backSlash = "\";
for(vector<string>::const_iterator j = folders.begin(); j != folders.end(); ++j)
{
pathToCreate += j->c_str() + backSlash;
if (pathToCreate.find(".txt") == std::string::npos)
CreateBackupFolders(pathToCreate);
}
if (MoveFile(lastExportedFilePath.c_str(),destinationFilePath.c_str()))
AfxMessageBox("moved");
else
{
DWORD dw = GetLastError();
AfxMessageBox("Couldn't move");
}
lastExportedFilePath = i->FilePath;
}
catch(...)
{
Log(LOG_PATH_LOG_INSERTING, LOG_TYPE_EXCEPTION, "ExportLogsToDB()", "Move", destinationFilePath, "");
}
使用MoveFile
而不是CopyFile
和不使用MoveFileEx
的原因。根据微软的MSDN文档,它逐字声明,当您使用MoveFile
时,它不会复制安全描述,并且您将丢失到新位置的默认值,这是旁注。
但是由于它返回0意味着失败,我们可以通过调用GetLastError
API并查找结果数字到适当的错误字符串来扩展该错误,因此它更有意义。除了像这样在黑暗中随意开枪。此外,这个项目是否使用Unicode/Ansi或Not-Set作为默认字符集?
而且,在没有给我们一个回归测试的情况下发布整个问题,这样我们就可以评估它并告诉你哪里出了问题,这不是很有帮助。你想让别人快速解决你的问题,同时这对你和其他人来说也是一个学习的地方。
相关文章:
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 在其他文件中创建类时在 c++ 项目中不起作用
- g++ -I 和 -L 命令来查找不起作用的 .hpp 文件
- 该程序将.csv文件中的一系列单词放入数组中,然后随机生成句子.但它不起作用
- 无法打开包含文件'Graphics.hpp'没有这样的文件或目录,Visual Studio的其他包含不起作用
- 从类中的另一个文件请求函数不起作用
- 即使我正在.cpp文件中实例化一个伪对象,.cpp文件内模板函数的定义也不起作用
- 简单的二分搜索对我不起作用。我正在尝试从文本文件中搜索单词
- SFML 加载从文件不起作用,奇怪的错误
- C++密钥和文件的映射不起作用
- 从 TXT 文件中删除行不起作用
- QT QML:使用.qrc文件将SQLite数据库部署到Android不起作用
- bcdedit 文件输出不起作用,但其他命令可以
- 文件写入在PHP扩展c / c ++(linux)中不起作用
- Makefile在Linux中工作,但在Windows下不起作用,在子目录中找不到文件
- 使用 libcurl C++将文件下载到 Ubuntu,简单示例不起作用
- 尝试读取数据文件,存储在数组中并打印所有元素,但它不起作用
- 为什么写入文件不起作用
- 包括不起作用C 的文件
- 在 C++ 中,.h 文件包含文件但不起作用