C++中CopyFile和MoveFile函数使用区别的示例分析

  //MyCopyFile.cpp#include

  #include

  #include

  #include "io.h"

  #include

  #include

  //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& files, vector& filesname);

  int main(void)

  {

  vector 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 files;

  vector 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.

  ");

  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& files, vector& 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);

  }

  }