C++ CopyFile()

关于CopyFile function,参考:https://msdn.microsoft.com/en-us/library/windows/desktop/aa363851(v=vs.85).aspx

IDE: Code::Blocks 16.01

操作系统:Windows 7 x64

 1 #include <windows.h>
 2 #include <stdio.h>
 3 
 4 int main()
 5 {
 6     DWORD dwError;
 7 
 8     if(CopyFile("H:\C++\NewDirectory\file.txt", "H:\C++\NewDirectory\del.bat", FALSE)) {
 9         printf("Copy file succeeds. 
");
10     }
11     else {
12         dwError = GetLastError();
13         switch(dwError) {
14         case ERROR_FILE_NOT_FOUND:
15             printf("The source file does not exist. 
");
16             printf("Error: %ld 
", dwError);
17             break;
18         default:
19             printf("The new file already exists. 
");
20             printf("Error: %ld 
", dwError);
21             break;
22         }
23     }
24 
25     return 0;
26 }

IDE: Microsoft Visual Studio Community 2017 15.5.2

操作系统:Windows 7 x64

 1 #include "stdafx.h"    /* IDE自行创建的 */
 2 
 3 #include <windows.h>
 4 
 5 int main(int argc, char **argv)
 6 {
 7     DWORD dwError;
 8 
 9     if (CopyFile(L"H:\C++\NewDirectory\file.txt", L"H:\C++\NewDirectory\del.bat", FALSE)) {
10         printf("Copy file succeeds. 
");
11     }
12     else {
13         dwError = GetLastError();
14         switch (dwError) {
15         case ERROR_FILE_NOT_FOUND:
16             printf("The source file does not exist. 
");
17             printf("Error: %ld 
", dwError);
18             break;
19         default:
20             printf("The new file already exists. 
");
21             printf("Error: %ld 
", dwError);
22             break;
23         }
24     }
25 
26     getchar();
27 
28     return 0;
29 }

功能:将位于H:C++NewDirectory目录中的源文件file.txt,复制到H:C++NewDirectory目录中,并命名为del.bat

原文地址:https://www.cnblogs.com/Satu/p/8207304.html