C++筆記之CopyFile和MoveFile的使用
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> intmain() { 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的使用