C/C++实现遍历文件夹中所有文件并复制到另一个路径
- 入口函数:backuppic
#include <stdlib.h>
#include <io.h>
#include <vector>
#include <stdio.h>
#include <napi.h>
#include <uv.h>
#include <string>
#include <cstring>
#include <vector>
void copy(char* tfrom, char* tto) {
FILE* fpread = fopen(tfrom, "rb"); //FILE结构体类型的文件指针fp 只读 二进制文件
FILE* fpwrite = fopen(tto, "wb"); //只写 二进制文件
//std::cout << "open" << std::endl;
if (fpread == NULL) {
perror("fpread error");
}
if (fpwrite == NULL) {
perror("fpwrite error");
}
char* p;
p = (char*)malloc(sizeof(char));//为指针申请内存空间
while (!feof(fpread)) { //feof()检测光标后是否还有内容 没有则返回非0
fread(p, sizeof(char), 1, fpread);
fwrite(p, sizeof(char), 1, fpwrite);
}
if (p != NULL){
free(p);
p = NULL;
}
fclose(fpread);
fclose(fpwrite);
return;
}
void DB::getFiles(std::string path, vector<std::string>& files, vector<std::string>& filesname) {
intptr_t hFile = 0;
struct _finddata_t fileinfo;
std::cout << path << std::endl;
string p;
if ((hFile = _findfirst(p.assign(path).append("\\*.bmp").c_str(), &fileinfo)) != -1) {
do {
files.push_back(p.assign(path).append("\\").append(fileinfo.name));
filesname.push_back(fileinfo.name);
} while (_findnext(hFile, &fileinfo) == 0);
_findclose(hFile);
}
}
void backuppic(std::string from, std::string to) {
vector<std::string> files;
vector<std::string> filesname;
std::cout << "getting filesname" << std::endl;
getFiles(from, files, filesname);
int size = files.size();
char* tf = new char[256];
char* tn = new char[256];
std::string target;
std::cout << "copying files" << std::endl;
for (int i = 0; i < size; i++) {
target = to + filesname[i];
strcpy(tf, files[i].c_str());
strcpy(tn, target.c_str());
copy(tf, tn);
}
if (tn != NULL) {
free(tn);
tn = NULL;
}
if (tf != NULL) {
free(tf);
tf = NULL;
}
std::cout << "backupover" << std::endl;
return;
}
编写时的几个问题:
-
运行时提前终止
原因:
-
没有释放指针
所有动态分配new出的指针都要free,并且置为NULL。
char* p;
p = (char*)malloc(sizeof(char));//为指针申请内存空间
if (p != NULL){
free(p);
p = NULL;
}
- 循环嵌套使得动态分配指针太多,尽量减少循环嵌套,指针提到最外面。
- string转char*的方法。尽量减少使用。
string p="zxy";
int len = sizeof(p);
char* t = new char[len];
strcpy(t, p.c_str());
错误方法:
string p="zxy";
char* t = p.c_str();
原因:p.c_str()被释放后,t会成为野指针,报错。
3. _findnext报错
句柄不要用
long hFile=0;
改成
intptr_t hFile=0;
- 遍历文件夹时,读取的第一个文件属性显示是文件夹,但其实文件夹里只有bmp文件,以至于向下运行一直报错。
方法:修改路径只遍历bmp文件:
string path="F:\\testcopy\\*.bmp";
遍历路径下所有内容:
string path="F:\\testcopy\\*";
- 遍历文件夹内所有文件(文件夹和各种文件)
修改getFiles函数为:
void getFiles(string path, vector<string>& files, vector<string>& filesname) {
//文件句柄
intptr_t hFile = 0;
//文件信息
struct _finddata_t fileinfo;
string p;
if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1) {
do {
if ((fileinfo.attrib & _A_SUBDIR)) { //递归遍历文件夹
if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
getFiles(p.assign(path).append("\\").append(fileinfo.name), files,filesname);
}
else {
files.push_back(p.assign(path).append("\\").append(fileinfo.name));
filesname.push_back(fileinfo.name);
}
} while (_findnext(hFile, &fileinfo) == 0);
_findclose(hFile);
}
}