加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C fwrite()写的比预期的要多

发布时间:2020-12-16 10:25:36 所属栏目:百科 来源:网络整理
导读:复制文件时遇到问题 码: bool done; FILE* fin; FILE* fout; const int bs = 1024*64;//64 kb char* buffer[bs]; int er,ew,br,bw; long long int size = 0; long long int sizew = 0; er = fopen_s(fin,s.c_str(),"rb"); ew = fopen_s(fout,s2.c_str(),"wb
复制文件时遇到问题

码:

bool done;
    FILE* fin;
    FILE* fout;
    const int bs = 1024*64;//64 kb
    char* buffer[bs];
    int er,ew,br,bw;
    long long int size = 0;
    long long int sizew = 0;
    er = fopen_s(&fin,s.c_str(),"rb");
    ew = fopen_s(&fout,s2.c_str(),"wb");
    if(er == 0 && ew == 0){
        while(br = fread(buffer,1,bs,fin)){
            size += br;
            sizew += fwrite(buffer,fout);
        }
        done = true;
    }else{
        done = false;
    }
    if(fin != NULL)fclose(fin);
    if(fout != NULL)fclose(fout);

以某种方式fwrite写入整个缓冲区忽略计数值(br)

一些例子如何:

Copying 595 file of 635 DONE. 524288/524288 B
Copying 596 file of 635 DONE. 524288/524288 B
Copying 597 file of 635 DONE. 65536/145 B
Copying 598 file of 635 DONE. 65536/16384 B
Copying 599 file of 635 DONE. 65536/145 B
Copying 600 file of 635 DONE. 65536/67 B
Copying 601 file of 635 DONE. 65536/32768 B
Copying 602 file of 635 DONE. 65536/67 B

谁知道问题出在哪里?

解决方法

你应该做

sizew += fwrite(buffer,fout);

你正在传递bs,这是允许fread读取的最大数量. br是fread实际读取的数量.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读