Nov 16, 2014 01:26 AM
(This post was last modified: Nov 16, 2014 01:26 AM by IAmNotHere.)
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
(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.
ever thought about WIN32_LEAN_AND_MEAN?
CopyFile is a Kernel32 function, i.e. it's already present without any "included" extra libraries. The call itself costs a few bytes, not kBytes. So obviously something is seriously going wrong with your project, but without seeing the source (as IAmNotHere already wrote), there is no way to understand what exactly causes that behaviour.
Nov 18, 2014 12:11 AM
(This post was last modified: Nov 18, 2014 10:05 AM by IAmNotHere.)
I replaced :
Code bool copyFile(const char SRC[], const char DEST[])
{
std::ifstream src;
std::ofstream dest;
src.open(SRC, std::ios::binary);
dest.open(DEST, std::ios::binary);
if (!src.is_open() || !dest.is_open())
return false;
dest << src.rdbuf();
dest.close();
src.close();
return true;
}
with:
Code BOOL WINAPI CopyFile(
__in LPCTSTR lpExistingFileName,
__in LPCTSTR lpNewFileName,
__in BOOL bFailIfExists
);
and I have the filesize 100 KB smaller. And indeed the code doesn't run as supposed. On some machines the code runs and on some not at all.
IANH
DO NOT DEAL WITH THIS MEMBER
Nov 18, 2014 01:46 PM
(This post was last modified: Nov 18, 2014 01:48 PM by Mouse❤️.)
If you use <<, and your file contains a \0 null byte, what do you think happens when it runs into it? You need to know the size of your file and then copy just that many bytes, so find the size of the file with your ifstream by seekg()ing to the end of the file, tellg()ing your program how big the file is, then ostream::write()ing to the new file the contents of the old file that you istream::read() from. If that doesn't make any sense to you, then you better stop talking, and start posting code because you're not going to get anything from us if we don't know what the real problem is, because if you don't know, why the heck would we without seeing it?
Nov 18, 2014 02:20 PM
(This post was last modified: Nov 18, 2014 04:45 PM by IAmNotHere.)
Thanks, I have to re-look the whole source first myself.
I will take your advise along coding.
IAmNotHere
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
}
(Nov 19, 2014 05:13 PM)_Stoner Wrote: What do your #include header files look like? Code #include <windows.h>
#include "stdafx.h"
#include "structs.h"
#include "strings.h"
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
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.
I noticed already. I deleted one by one of the <headers> till my vs 2013 errored to see if the filesize changed.
Regards.
|