Reputation: 73
I have problem when copying files
code:
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,1,bs,fout);
}
done = true;
}else{
done = false;
}
if(fin != NULL)fclose(fin);
if(fout != NULL)fclose(fout);
Somehow fwrite writes whole buffer ignoring count value (br)
Some examples how:
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
Anyone knows where is the problem?
Upvotes: 3
Views: 221
Reputation: 385385
ignoring count value (br)
Actually you wrote bs
.
A good example of the dangers of poor variable naming!
Upvotes: 7
Reputation: 16310
You should do
sizew += fwrite(buffer,1,br,fout);
You were passing bs
, which was the maximum amount that fread
was allowed to read. br
was the amount that fread
actually did read.
Upvotes: 5