Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C 

CopyFile

(Nov 16, 2014 01:26 AM)IAmNotHere Wrote:  Hello,

I have a source where I need to copy a file to a folder, but the CopyFile function I insert in my project almost doubles the filesize after compiling.
The ouput size grows from 500 kb to 1 MB!
Are there less size consuming alternatives?

Regards,
IANH

Post the source? Noone can help you without seeing what you are doing.
What do your #include header files look like?
You are obviously doing something wrong so here you go.

This was slapped together so you may want to add more error checking or fix some syntax errors (if there are any)
Code
int copyFile(char *src, char *dst)
{
     int size = 0;
     char *buff;
     FILE *in = NULL;
     FILE *out = NULL;

     in = fopen(src, "rb");
     out = fopen(dst, "wb+");

     if(!in || !out)
      return -1; //Failed

     fseek(in, 0, SEEK_END);
     size = ftell(in);//Get total bytes
     fseek(in, 0, SEEK_SET);//Back to beginning

     buff = (char*)malloc(sizeof(char)*size);//Make buffer large enough to hold our data
     memset(buff, 0, sizeof(char)*size);//Init buffer to all 0

     fread(buff, sizeof(char), size, in);//Read it all into buffer
     fwrite(buff, sizeof(char), size, out);//Output buffe

     fclose(in); //Close files
     fclose(out);
     free(buff); //Deallocate buffer

     return 1; //Success

}
That would be why your file size is so huge. 🤝
iostream is huge, as are some of the other headers you are using. If you want a small file size, you should look into wanting to code natively instead of using standard libraries.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by zapfenApr 23, 2013 02:07 PM 1 84 Apr 23, 2013 02:07 PM
Last Post: Shihanus
[.NET] by FreckleSSep 30, 2010 07:47 AM 0 52 Sep 30, 2010 07:47 AM
Last Post: FreckleS