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

复制文件时遇到问题

码:

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实际读取的数量.

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...