1、函数定义

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

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

2.函数原型

CopyFile:

MoveFile:

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

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

3、Demo

示例一:

CopyFile:

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

CopyFile:

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

示例二:

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

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

//MyCopyFile.cpp#include <iostream>
#include <stdio.h>
#include <opencv2/opencv.hpp>
#include "io.h"
#include <fstream>
#include <WINDOWS.H>

//define the buffer size. Do not change the size!
#define DETECT_BUFFER_SIZE 0x20000
using namespace std;

//getFiles_Name函数声明,作用:读取path路径下的.png格式文件,并将每个.png文件的路径和文件名分别存储到files和filesname
void getFiles_Name(string path, vector<string>& files, vector<string>& filesname);

int main(void)
{
 vector<string> classnames;
 classnames.push_back(string("disgust"));
 classnames.push_back(string("neutral"));
 classnames.push_back(string("scream"));
 classnames.push_back(string("smile"));
 classnames.push_back(string("squint"));
 classnames.push_back(string("surprise"));

 for (int iexpress = 0; iexpress < 7;iexpress++)
 {
  string inputStr = "C:\SourceFile\" + classnames[iexpress];
  string outputStr = "C:\TargetFile\" + classnames[iexpress] + "\";

  vector<string> files;
  vector<string> filesname;
  ////获取该路径下的所有文件 
  getFiles_Name(inputStr, files, filesname);

  //循环复制文件
  for (int k = 0; k < files.size(); k++)
  {
   unsigned char *pBuffer = (unsigned char *)malloc(DETECT_BUFFER_SIZE);
   if (!pBuffer)
   {
    fprintf(stderr, "Can not alloc buffer.n");
    return -1;
   }

   cout << files[k] << endl;
   CopyFile(files[k].c_str(), (outputStr + filesname[k]).c_str(), FALSE);//false代表覆盖,true不覆盖
      //若文件路径为string类型变量,例如为pathstr,则需使用pathstr.c_str()转换即可;
   free(pBuffer);
  }
 }
 return 0;
}

void getFiles_Name(string path, vector<string>& files, vector<string>& filesname)
{
 //文件句柄 
 intptr_t hFile;
 //文件信息,声明一个存储文件信息的结构体 
 struct _finddata_t fileinfo;
 string p;//字符串,存放路径
    //string name;
 if ((hFile = _findfirst(p.assign(path).append("\*.png").c_str(), &fileinfo)) != -1)//若查找成功,则进入
 {
  do
  {
   files.push_back(path + "\" + fileinfo.name);
   filesname.push_back(fileinfo.name);
  } while (_findnext(hFile, &fileinfo) == 0);
  //_findclose函数结束查找
  _findclose(hFile);
 }
}

如果出现以下错误:

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

解决方法:

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

错误原因:

因为我的程序在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函数的区别内容请搜索悠悠之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持悠悠之家!

点赞(72)

评论列表共有 0 条评论

立即
投稿
返回
顶部