C&C++——大文件拷贝【简单实现】

大文件拷贝

C++实现

需要iomanip、fstream头文件支持

typedef long long ll;
#define EACH_BLOCK (1024 * 8) // 一次读取的大小

void copy(const string& fileName) {
	string copyFileName = fileName;
	int pos = copyFileName.rfind(".");
	copyFileName.insert(pos, ".bp");
	
	ifstream ifs(fileName.c_str(), ios::binary);
	if (!ifs.is_open()) {
		cout << "原文件打开失败" << endl;
		return;
	}
	ofstream ofs(copyFileName.c_str(), ios::binary);
	if (!ofs.is_open()) {
		cout << "目标文件打开失败" << endl;
	}
	// 得到原文件的大小
	ifs.seekg(0, ios::end);
	ll fileSize = ifs.tellg();
	ifs.seekg(0, ios::beg);
	char buffer[EACH_BLOCK] = { 0 };
	// 开始拷贝
	while (!ifs.eof()) {
		ifs.read(buffer, EACH_BLOCK); // 指定读取大小
		// gcount 返回上次从文件流提取出的字符个数。
		ll len = ifs.gcount(); // 实际读取的大小
		ofs.write(buffer, len);
		ofs.flush(); // 刷新来确保写入
		ll curSize = ofs.tellp();
		cout << fixed << setprecision(2) << "文件总大小: " << ((double)fileSize / 1024 / 1024) \ 
			<< "MB;已下载: " << ((double)curSize / 1024 / 1024) << "MB;当前进度: " \
			<< ((double)curSize / fileSize) * 100 << "%" << endl;
	}
	ifs.close();
	ofs.close();
}

C语言实现

如需实现像C++一样传入文件路径,可以自行修改

#define EACH_BLOCK (1024 * 8) // 一次读取的大小

void copy() {
	FILE* fp = nullptr;
	fopen_s(&fp, "D:\\software\\c++\\clion\\CLion-2022.1.2.tar.gz", "rb");
	if (fp == nullptr) {
		return;
	}
	fseek(fp, 0, SEEK_END);
	long totalSize = ftell(fp);
	rewind(fp);
	
	FILE* destFp = nullptr;
	fopen_s(&destFp, "D:\\software\\c++\\clion\\CLion-2022.1.2.bp.tar.gz", "wb");
	if (destFp == nullptr) {
		return;
	}
	
	long currRead = 0;
	while (!feof(fp)) {
		char buffer[EACH_BLOCK] = { 0 };
		fread(buffer, sizeof(char), sizeof(buffer), fp);
		currRead = ftell(fp);
		int size = currRead % EACH_BLOCK == 0 ? EACH_BLOCK : currRead % EACH_BLOCK;
		fwrite(buffer, sizeof(char), size, destFp);
		fflush(destFp);
		printf_s("总大小:%.2fM 已完成:%.2fM 当前进度:%.2f%%\n",
		(float)totalSize / 1024 / 1024,
		(float)currRead / 1024 / 1024,
		(float)currRead / totalSize * 100);
	}
	
	fclose(fp);
	fclose(destFp);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值