C++中CopyFile和MoveFile函數(shù)使用區(qū)別的示例分析
1、函數(shù)定義
CopyFile(A, B, FALSE);表示將文件A拷貝到B,如果B已經(jīng)存在則覆蓋(第三參數(shù)為TRUE時(shí)表示不覆蓋)
MoveFile(A, B);表示將文件A移動(dòng)到B
2.函數(shù)原型
CopyFile:

MoveFile:

由函數(shù)原型可以看出,這兩個(gè)函數(shù)的前兩個(gè)輸入?yún)?shù)都為L(zhǎng)RCWSTR類型,如果我們定義的是char*,記得轉(zhuǎn)換成LRCWSTR,否則會(huì)報(bào)錯(cuò);
另外,這兩個(gè)函數(shù)都返回一個(gè)bool型變量,表示執(zhí)行成功與否,當(dāng)目標(biāo)位置路徑不存在時(shí),會(huì)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:如果目標(biāo)位置已經(jīng)存在同名文件,就覆蓋,return 1
//TRUE:如果目標(biāo)位置已經(jīng)存在同名文件,則補(bǔ)拷貝,return 0
//后者路徑若不錯(cuò)在,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:將前者移動(dòng)到后者中(后者路徑若不錯(cuò)在,return 0)
system("pause");
return 1;
}
示例二:
#include <WINDOWS.H>
int main()
{
char *sourcefile = "d://source//p.png";//源文件
char *targetfile = "d://target//q.png";//目標(biāo)文件
CopyFile(sourcefile , targetfile , FALSE);//false代表覆蓋,true不覆蓋
return 0;
}
4、將圖片批量復(fù)制到另一個(gè)文件夾
//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函數(shù)聲明,作用:讀取path路徑下的.png格式文件,并將每個(gè).png文件的路徑和文件名分別存儲(chǔ)到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);
//循環(huán)復(fù)制文件
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()轉(zhuǎn)換即可;
free(pBuffer);
}
}
return 0;
}
void getFiles_Name(string path, vector<string>& files, vector<string>& filesname)
{
//文件句柄
intptr_t hFile;
//文件信息,聲明一個(gè)存儲(chǔ)文件信息的結(jié)構(gòu)體
struct _finddata_t fileinfo;
string p;//字符串,存放路徑
//string name;
if ((hFile = _findfirst(p.assign(path).append("\\*.png").c_str(), &fileinfo)) != -1)//若查找成功,則進(jìn)入
{
do
{
files.push_back(path + "\\" + fileinfo.name);
filesname.push_back(fileinfo.name);
} while (_findnext(hFile, &fileinfo) == 0);
//_findclose函數(shù)結(jié)束查找
_findclose(hFile);
}
}
如果出現(xiàn)以下錯(cuò)誤:

不能從const char*轉(zhuǎn)換為L(zhǎng)PCWSTR的原因及解決方法:
解決方法:
項(xiàng)目-->2.MyCopyFile屬性-->3.配置屬性-->4.常規(guī)-->5.字符集:改成 未設(shè)置


錯(cuò)誤原因:
因?yàn)槲业某绦蛟赨NICODE(寬字節(jié))字符集下運(yùn)行, UNICODE與ANSI有什么區(qū)別呢?簡(jiǎn)單的說(shuō),UNICODE版的字符比ANSI 的內(nèi)存占用大,比如:Win32程式中出現(xiàn)的標(biāo)準(zhǔn)定義 char 占一個(gè)字節(jié),而 char 的UNICODE版被定義成這樣: typedef unsigned short wchar_t ;占2個(gè)字節(jié)。 所以有字符做參數(shù)的函數(shù)相應(yīng)也用兩個(gè)版本了。
參考博客:
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
到此這篇關(guān)于C++中CopyFile和MoveFile函數(shù)的區(qū)別的文章就介紹到這了,更多相關(guān)C++中CopyFile和MoveFile函數(shù)的區(qū)別內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- C++ move()函數(shù)案例詳解
- C++11中std::move、std::forward、左右值引用、移動(dòng)構(gòu)造函數(shù)的測(cè)試問(wèn)題
- C++11中value category(值類別)及move semantics(移動(dòng)語(yǔ)義)的介紹
- C++中的移動(dòng)構(gòu)造函數(shù)及move語(yǔ)句示例詳解
- C++11右值引用和std::move語(yǔ)句實(shí)例解析(推薦)
- C++中memcpy和memmove的區(qū)別總結(jié)
- 基于C++ list中erase與remove函數(shù)的使用詳解
- C++ CopyFile,MoveFile用法案例詳解
相關(guān)文章
C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)與算法之圖的遍歷(一)
這篇文章主要是介紹了利用深度優(yōu)先算法實(shí)現(xiàn)圖的遍歷,文中利用圖文詳細(xì)的介紹了實(shí)現(xiàn)步驟,對(duì)我們學(xué)習(xí)數(shù)據(jù)結(jié)構(gòu)與算法有一定的幫助,需要的朋友可以參考一下2021-12-12
c語(yǔ)言實(shí)現(xiàn)基數(shù)排序解析及代碼示例
這篇文章主要介紹了c語(yǔ)言實(shí)現(xiàn)基數(shù)排序解析及代碼示例,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12
Visual Studio中scanf函數(shù)報(bào)錯(cuò)的幾種解決方法
本文主要介紹了Visual Studio中scanf函數(shù)報(bào)錯(cuò)的幾種解決方法,文中通過(guò)圖文示例介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-03-03
C語(yǔ)言程序設(shè)計(jì)第五版譚浩強(qiáng)課后答案(第二章答案)
這篇文章主要介紹了C語(yǔ)言程序設(shè)計(jì)第五版譚浩強(qiáng)課后答案(第二章答案),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2021-04-04
用C語(yǔ)言實(shí)現(xiàn)鏈?zhǔn)綏=榻B
大家好,本篇文章主要講的是用C語(yǔ)言實(shí)現(xiàn)鏈?zhǔn)綏=榻B,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12

