c - Why this way of copying files isn't working -



c - Why this way of copying files isn't working -

i'm writing wrapper help me in future projects (i finished c book), , want re-create file without using fgetc. here's code, doesn't work:

int copyfile(char* filename, char* dest) { file* fp, *fout; fp = fopen(filename,"rb"); //fout = fopen(dest, "wb"); if(fp == null) { homecoming -1; } /*while((c = fgetc(fp)) != eof) { fputc(c,fout); }*/ long size = getfilesize(fp); printf("%lu", size); char* file = malloc(size); fread(file, size, 1, fp); //fclose(fp); fout = fopen(dest, "wb"); fwrite(file, size, 1, fout); fclose(fp); fclose(fout); homecoming 0; }

i open files hexeditor , aren't similar. doing wrong?

the problem in getfilesize function, move file-pointer end, never rewind origin again.

that means fread function phone call not read anything, pointer @ end of file. what's written contents of allocated memory, indeterminate (and seemingly random).

if checked fread returned, have seen immediately.

let lesson on checking homecoming values of functions can fail in 1 way or other.

c file-copying

Comments

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -