大文件拷贝
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);
}