Jump to content
  • Advertisement

Archived

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

TheMatrixXXX

store a file into an archive

This topic is 5737 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement
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
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
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

  
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
BTW, it's not tested in any way, I wrote it all from top of my head.

[edited by - dalleboy on October 3, 2002 6:37:00 AM]

Share this post


Link to post
Share on other sites
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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!