C++中CopyFile和MoveFile函數(shù)使用區(qū)別的示例分析
1、函數(shù)定義
CopyFile(A, B, FALSE);表示將文件A拷貝到B,如果B已經(jīng)存在則覆蓋(第三參數(shù)為TRUE時表示不覆蓋)
MoveFile(A, B);表示將文件A移動到B
2.函數(shù)原型
CopyFile:
MoveFile:
由函數(shù)原型可以看出,這兩個函數(shù)的前兩個輸入?yún)?shù)都為LRCWSTR類型,如果我們定義的是char*,記得轉(zhuǎn)換成LRCWSTR,否則會報錯;
另外,這兩個函數(shù)都返回一個bool型變量,表示執(zhí)行成功與否,當目標位置路徑不存在時,會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:如果目標位置已經(jīng)存在同名文件,就覆蓋,return 1 //TRUE:如果目標位置已經(jīng)存在同名文件,則補拷貝,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函數(shù)聲明,作用:讀取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); //循環(huán)復制文件 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; //文件信息,聲明一個存儲文件信息的結(jié)構體 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函數(shù)結(jié)束查找 _findclose(hFile); } }
如果出現(xiàn)以下錯誤:
不能從const char*轉(zhuǎn)換為LPCWSTR的原因及解決方法:
解決方法:
項目-->2.MyCopyFile屬性-->3.配置屬性-->4.常規(guī)-->5.字符集:改成 未設置
錯誤原因:
因為我的程序在UNICODE(寬字節(jié))字符集下運行, UNICODE與ANSI有什么區(qū)別呢?簡單的說,UNICODE版的字符比ANSI 的內(nèi)存占用大,比如:Win32程式中出現(xiàn)的標準定義 char 占一個字節(jié),而 char 的UNICODE版被定義成這樣: typedef unsigned short wchar_t ;占2個字節(jié)。 所以有字符做參數(shù)的函數(shù)相應也用兩個版本了。
參考博客:
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函數(shù)的區(qū)別的文章就介紹到這了,更多相關C++中CopyFile和MoveFile函數(shù)的區(qū)別內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
C語言數(shù)據(jù)結(jié)構進階之棧和隊列的實現(xiàn)
棧和隊列,嚴格意義上來說,也屬于線性表,因為它們也都用于存儲邏輯關系為 "一對一" 的數(shù)據(jù),但由于它們比較特殊,因此將其單獨作為一章,做重點講解2021-11-11C語言實現(xiàn)opencv提取直線、輪廓及ROI實例詳解
這篇文章主要介紹了C語言實現(xiàn)opencv提取直線、輪廓及ROI實例詳解,具有一定借鑒價值,需要的朋友可以參考下2018-01-01C++中的new/delete、構造/析構函數(shù)、dynamic_cast分析
這篇文章主要介紹了C++中的new/delete、構造/析構函數(shù)、dynamic_cast分析 本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-05-05