user90714
user90714

Reputation: 153

MFC problem to copy binary file

I want to copy a binary master file in a new binary file. This file contain nothing but have a predefined size (20000 lines).

Here what i'm doing:

     FILE *A_Lire;
     FILE *A_Creer;

A_Lire = fopen(MASTERPath,"rb");
A_Creer = fopen(PARTPRGPath, "wb");

fseek(A_Lire,0,SEEK_END);
int end = ftell(A_Lire);

char* buf = (char*)malloc(end);

fread(buf,sizeof(char),end,A_Lire);
fwrite(buf,sizeof(char),end,A_Creer);

fclose(A_Creer);
fclose(A_Lire);

This code create the new file with the good size but this is not exactly the same file because I'm not able to used this new file like the master. Something is different, maybe corrupted, maybe the way to write in the file ???

Do you have any idea ???

I think this is MFC code

Thanks,

Upvotes: 0

Views: 551

Answers (1)

rossoft
rossoft

Reputation: 2182

when you do fseek(..SEEK_END), the position inside the opened file is at the end, whenever you call fread, you are getting 0 bytes as you're at the end.

Just do a rewind after that:

fseek(A_Lire,0,SEEK_END);

int end = ftell(A_Lire);

fseek(A_Lire,0,SEEK_SET);

Upvotes: 2

Related Questions