C/C++实现遍历文件夹中所有文件并复制到另一个目录

3 篇文章 0 订阅

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;
}

编写时的几个问题:

  1. 运行时提前终止

    原因:

  • 没有释放指针

    所有动态分配new出的指针都要free,并且置为NULL。

	char* p;
	p = (char*)malloc(sizeof(char));//为指针申请内存空间 
	if (p != NULL){		
		free(p);		
		p = NULL;
	}
  • 循环嵌套使得动态分配指针太多,尽量减少循环嵌套,指针提到最外面。
  1. 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;

  1. 遍历文件夹时,读取的第一个文件属性显示是文件夹,但其实文件夹里只有bmp文件,以至于向下运行一直报错。
    方法:修改路径只遍历bmp文件:
string	path="F:\\testcopy\\*.bmp";

遍历路径下所有内容:

string	path="F:\\testcopy\\*";
  1. 遍历文件夹内所有文件(文件夹和各种文件)
    修改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);
	}
}
  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值