Archived

This topic is now archived and is closed to further replies.

TheMatrixXXX

store a file into an archive

Recommended Posts

TheMatrixXXX    122
hi all i´m currently programming a file packaging tool for my engine and i have one problem: when i store a file into my package and extract it again the file is damaged. when i get the filesize in my app its the same like the windows explorer tells me and after extraction too. i tried it with a text file and saw that my file reading and writing code changes some bytes(like the space into a square symbol) so what i want to know is how to read a file in without damaging it? i hope you understand what i mean thx for help. --=[[TheMatrixXXX]]=--

Share this post


Link to post
Share on other sites
TheMatrixXXX    122
ok here´s an example:


  
unsigned char *FileData;
FILE *in .....
int FileSize = GetFileSize(in);

FileData = (unsigned char*)malloc(FileSize);
fread(FileData, 1, FileSize, in);

fwrite(FileData, 1, FileSize, package);

delete FileData;
FileData = NULL;

fclose(in);


maybe you can tell me how you would copy a file with a method like that.

thx for help

--=[[TheMatrixXXX]]=--

Share this post


Link to post
Share on other sites
dalleboy    324
I see nothing wrong with that except that you delete something that you have malloc:ed. Use free for malloc, realloc and calloc, use delete for new, use delete [] for new [].

Share this post


Link to post
Share on other sites
TheMatrixXXX    122
thx
do you think the unsigned char could be a problem?
do you have code to copy a file? maybe it would help to see something different.

thx for you replies

--=[[TheMatrixXXX]]=--

Share this post


Link to post
Share on other sites
dalleboy    324

  
int CopyFile(const char* szIn, const char* szOut)
{
FILE* in = fopen(szIn, "rb");
if (in == NULL)
{
return 0;
}

FILE* out = fopen(szOut, "wb");
if (out == NULL)
{
fclose(in);
return 0;
}

const size_t BUFFER_SIZE = 1024;
char buffer[BUFFER_SIZE];

size_t bytesRead;

while (!feof(in))
{
bytesRead = fread(buffer, 1, BUFFER_SIZE, in);
if (ferror(in))
{
fclose(in);
fclose(out);
return 0;
}

if (fwrite(buffer, 1, BUFFER_SIZE, bytesRead) < bytesRead)
{
fclose(in);
fclose(out);
return 0;
}
}

fclose(in);
fclose(out);
return 1;
}

Share this post


Link to post
Share on other sites
TheMatrixXXX    122
ok i know now that the problem is not when is copy the file into the archive. i think i do something wrong when i extract the file again, but i don´t know what because the filesize is correct and the file is almost correct
in text files the text formation is damaged. the file looks very different when i open it again in notepad and compare it with the original file.

--=[[TheMatrixXXX]]=--

Share this post


Link to post
Share on other sites
llyod    122
Are you JUST trying to copy a file in/out to/from an archive? Or are you doing something else with it in between?

2DNow - Specializing in yesterday''s technology today!

Share this post


Link to post
Share on other sites
TheMatrixXXX    122
no i just copy the file out of my archive
i have a "header" structure with the position and the length.
when i want to extract i search for the header and then seek to the position and copy out the data.

--=[[TheMatrixXXX]]=--

Share this post


Link to post
Share on other sites