c++用文件流实现文件拷贝

之前用c++作后台的时候碰到一个问题:返回一张图片数据。查了一些资料终于解决了。核心就是文件流怎么读写的问题,已经将问题简化为文件拷贝,代码如下:

#include <iostream>
#include <fstream>
using namespace std;
int main()
{
    cout << "Content-Type:image/png\r\n\r\n";
    fstream file;
    file.open("1.png", ios::binary|ios::in|ios::ate);   //打开时指针在文件尾
    int length = file.tellg();
    char* imgData = new char[length];
    file.seekg(0);
    file.read(imgData, length);  //二进制只能用这个读
    
    fstream file2;
    file2.open("2.png", ios::binary|ios::out);
    file2.write(imgData, length);   //二进制只能用这个写
    cout << "ok";
    
    return 0;
}

结果:
在这里插入图片描述
原来是不存在2.png的

----------------------------------------华丽的分割线---------------------------------------------
事实上,以上代码非常辣鸡,存在以下问题:

  • 使用了int, 不支持2G以上的文件
  • 使用了new申请内存并且没有释放,可能造成程序崩溃
  • 没有封装,不方便使用

现修改如下:

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

/* 复制文件
 * @参数 src - 源文件名
 * @参数 dest - 目标文件名,如果目标文件已存在,则覆盖
 * @返回 true | false 代表拷贝是否成功
*/
bool copyFile(string src, string dest) {
    ifstream is(src, ios::binary);
    if (is.fail()) {
        return false;
    }

    ofstream os(dest, ios::binary);
    if (os.fail()) {
        return false;
    }

    is.seekg(0, ios::end);
    long long length = is.tellg();  // C++ 支持的最大索引位置
    is.seekg(0);
    char buf[2048];
    while (length > 0)
    {
        int bufSize = length >= 2048 ? 2048 : length;
        is.read(buf, bufSize);
        os.write(buf, bufSize);
        length -= bufSize;
    }

    is.close();
    os.close();
    return true;
}


int main() {
    if (copyFile("./ubuntu-19.10-live-server-amd64.iso", "./test_cp.iso")) {
        cout << "复制文件成功" << endl;
    }
    else {
        cout << "复制文件失败" << endl;
     }
    return 0;
}

在这里插入图片描述
拷贝大文件轻轻松松。

复制时间的思考

复制文件所需要的时间与磁盘本身、buf的大小都有很大关系。本文分别对大小为347.4M和4.7G的文件进行了拷贝实验。实验条件为:

设备型号及参数
CPUIntel Core m3-6Y30
内存4GB(4GB×1) LPDDR3(低功耗版)1866MHz
硬盘型号为 AMSUNG MZNTY128 100Q 容量为 128G SSD

针对不同buf大小,得到拷贝用时如下。1-time和2-time分别对应347.4M和4.7G的文件。 可以发现,当buf大小增大到一定程度时对拷贝需要的时间趋于稳定。

在这里插入图片描述
实验代码如下:

#include <iostream>
#include <string>
#include <fstream>
#include<ctime>

using namespace std;

/* 复制文件
 * @param src  源文件名
 * @param dest 目标文件名,如果目标文件已存在,则覆盖
 * @return true | false 代表拷贝是否成功
*/
bool copyFile(string src, string dest) {
    const int BUF_SIZE = 32768;
    ifstream is(src, ios::binary);
    if (is.fail()) {
        return false;
    }

    ofstream os(dest, ios::binary);
    if (os.fail()) {
        return false;
    }

    is.seekg(0, ios::end);
    long long length = is.tellg();  // C++ 支持的最大索引位置
    is.seekg(0);
    char buf[BUF_SIZE];
    while (length > 0)
    {
        int bufSize = length >= BUF_SIZE ? BUF_SIZE : length;
        is.read(buf, bufSize);
        os.write(buf, bufSize);
        length -= bufSize;
    }

    is.close();
    os.close();
    return true;
}


int main(){
    clock_t startTime = clock(), endTime;
    
    //copyFile("wps-office_11.1.0.10702_amd64.deb", "test.deb");
    copyFile("Windows 10 x64.iso", "test.iso");
    endTime = clock();
    cout << "The run time is:" <<(double)(endTime - startTime) / CLOCKS_PER_SEC << "s" << endl;

    return 0;
}
  • 15
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 11
    评论
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值