views:

82

answers:

2

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,

+2  A: 

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);
rossoft
+1, although I think it's SEEK_SET instead of SEEK_BEGIN
schnaader
Thanks you very much !!!!!!!
I agree, I'm editing the answer, thanks @schnaader
rossoft
That's right it's SEEK_SET and it's work perfectly !!!
A: 

see MSDN example how to copy binary files with CFile

ArsenMkrt