在我的应用程序中,我想将文件复制到另一个硬盘,所以这是我的代码:
#include <windows.h>
using namespace std;
int main(int argc, char* argv[] )
{
string Input = "C:\\Emploi NAm.docx";
string CopiedFile = "Emploi NAm.docx";
string OutputFolder = "D:\\test";
CopyFile(Input.c_str(), string(OutputFolder+CopiedFile).c_str(), TRUE);
return 0;
}
所以执行此操作后,它会在 D:
HDD 文件中显示我 testEmploi NAm.docx
但我希望他创建测试文件夹,如果它不存在。
我想在不使用 Boost 库的情况下做到这一点。
原文由 pourjour 发布,翻译遵循 CC BY-SA 4.0 许可协议
使用 WINAPI
CreateDirectory()
函数创建文件夹。您可以在不检查目录是否已存在的情况下使用此功能,因为它会失败,但
GetLastError()
将返回ERROR_ALREADY_EXISTS
:构建目标文件的代码不正确:
这将产生
"D:\testEmploi Nam.docx"
:目录和文件名之间缺少路径分隔符。示例修复: