当前位置:首页 > 通信资讯 > 正文

c++ copyfile函数(vc copyfile)

1、函数定义

CopyFile(A, B, FALSE);表示将文件A拷贝到B,如果B已经存在则覆盖(第三参数为TRUE时表示不覆盖)

MoveFile(A, B);表示将文件A移动到B

2.函数原型

CopyFile:

c++ copyfile函数(vc copyfile)

MoveFile:

c++ copyfile函数(vc copyfile)

由函数原型可以看出,这两个函数的前两个输入参数都为LRCWSTR类型,如果我们定义的是char*,记得转换成LRCWSTR,否则会报错;

另外,这两个函数都返回一个bool型变量,表示执行成功与否,当目标位置路径不存在时,会return 0

3、Demo

示例一:

CopyFile:

  1. #include <fstream>
  2. #include <windows.h>
  3. int main()
  4. {
  5. char *fn = "test.txt";
  6. std::ofstream out(fn);
  7. if (!out.is_open())
  8. return 0;
  9. out.close();
  10. WCHAR buf[256];
  11. memset(buf, 0, sizeof(buf));
  12. MultiByteToWideChar(CP_ACP, 0, fn, strlen(fn) + 1, buf, sizeof(buf) / sizeof(buf[0]));
  13. CopyFile(buf, L"../file/output.txt", FALSE);//FALSE:如果目标位置已经存在同名文件,就覆盖,return 1
  14. //TRUE:如果目标位置已经存在同名文件,则补拷贝,return 0
  15. //后者路径若不错在,return 0
  16. system("pause");
  17. return 1;
  18. }

CopyFile:

  1. #include <fstream>
  2. #include <windows.h>
  3. int main()
  4. {
  5. char *fn = "test.txt";
  6. std::ofstream out(fn);
  7. if (!out.is_open())
  8. return 0;
  9. out.close();
  10. WCHAR buf[256];
  11. memset(buf, 0, sizeof(buf));
  12. MultiByteToWideChar(CP_ACP, 0, fn, strlen(fn) + 1, buf, sizeof(buf) / sizeof(buf[0]));
  13. MoveFile(buf, L"../file/output.txt");//FALSE:将前者移动到后者中(后者路径若不错在,return 0)
  14. system("pause");
  15. return 1;
  16. }

示例二:

  1. #include <WINDOWS.H>
  2. int main()
  3. {
  4. char *sourcefile = "d://source//p.png";//源文件
  5. char *targetfile = "d://target//q.png";//目标文件
  6. CopyFile(sourcefile , targetfile , FALSE);//false代表覆盖,true不覆盖
  7. return 0;
  8. }

4、将图片批量复制到另一个文件夹

  1. //MyCopyFile.cpp#include <iostream>
  2. #include <stdio.h>
  3. #include <opencv2/opencv.hpp>
  4. #include "io.h"
  5. #include <fstream>
  6. #include <WINDOWS.H>
  7. //define the buffer size. Do not change the size!
  8. #define DETECT_BUFFER_SIZE 0x20000
  9. using namespace std;
  10. //getFiles_Name函数声明,作用:读取path路径下的.png格式文件,并将每个.png文件的路径和文件名分别存储到files和filesname
  11. void getFiles_Name(string path, vector<string>& files, vector<string>& filesname);
  12. int main(void)
  13. {
  14. vector<string> classnames;
  15. classnames.push_back(string("disgust"));
  16. classnames.push_back(string("neutral"));
  17. classnames.push_back(string("scream"));
  18. classnames.push_back(string("smile"));
  19. classnames.push_back(string("squint"));
  20. classnames.push_back(string("surprise"));
  21. for (int iexpress = 0; iexpress < 7;iexpress++)
  22. {
  23. string inputStr = "C:\\SourceFile\\" + classnames[iexpress];
  24. string outputStr = "C:\\TargetFile\\" + classnames[iexpress] + "\\";
  25. vector<string> files;
  26. vector<string> filesname;
  27. ////获取该路径下的所有文件
  28. getFiles_Name(inputStr, files, filesname);
  29. //循环复制文件
  30. for (int k = 0; k < files.size(); k++)
  31. {
  32. unsigned char *pBuffer = (unsigned char *)malloc(DETECT_BUFFER_SIZE);
  33. if (!pBuffer)
  34. {
  35. fprintf(stderr, "Can not alloc buffer.\n");
  36. return -1;
  37. }
  38. cout << files[k] << endl;
  39. CopyFile(files[k].c_str(), (outputStr + filesname[k]).c_str(), FALSE);//false代表覆盖,true不覆盖
  40.      //若文件路径为string类型变量,例如为pathstr,则需使用pathstr.c_str()转换即可;
  41. free(pBuffer);
  42. }
  43. }
  44. return 0;
  45. }
  46. void getFiles_Name(string path, vector<string>& files, vector<string>& filesname)
  47. {
  48. //文件句柄
  49. intptr_t hFile;
  50. //文件信息,声明一个存储文件信息的结构体
  51. struct _finddata_t fileinfo;
  52. string p;//字符串,存放路径
  53. //string name;
  54. if ((hFile = _findfirst(p.assign(path).append("\\*.png").c_str(), &fileinfo)) != -1)//若查找成功,则进入
  55. {
  56. do
  57. {
  58. files.push_back(path + "\\" + fileinfo.name);
  59. filesname.push_back(fileinfo.name);
  60. } while (_findnext(hFile, &fileinfo) == 0);
  61. //_findclose函数结束查找
  62. _findclose(hFile);
  63. }
  64. }

如果出现以下错误:

c++ copyfile函数(vc copyfile)

不能从const char*转换为LPCWSTR的原因及解决方法:

解决方法:

项目-->2.MyCopyFile属性-->3.配置属性-->4.常规-->5.字符集:改成 未设置

c++ copyfile函数(vc copyfile)

c++ copyfile函数(vc copyfile)

错误原因:

因为我的程序在UNICODE(宽字节)字符集下运行, UNICODE与ANSI有什么区别呢?简单的说,UNICODE版的字符比ANSI 的内存占用大,比如:Win32程式中出现的标准定义 char 占一个字节,而 char 的UNICODE版被定义成这样: typedef unsigned short wchar_t ;占2个字节。 所以有字符做参数的函数相应也用两个版本了。

参考博客:

https://blog.csdn.net/u012043391/article/details/77663644

https://blog.csdn.net/callmeado/article/details/21826679

https://www.cnblogs.com/dongsheng/p/3586418.html

https://blog.csdn.net/linjingtu/article/details/53190491

到此这篇关于C++中CopyFile和MoveFile函数的区别的文章就介绍到这了,更多相关C++中CopyFile和MoveFile函数的区别内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://www.cnblogs.com/yuehouse/p/10159027.html

如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

为您推荐:

发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。