store a file into an archive

Started by
11 comments, last by TheMatrixXXX 21 years, 6 months ago
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]]=--
--=[[TheMatrixXXX]]=--
Advertisement
Are you reading/writing the file in binary? If not, do so. An example would be nice.
Arguing on the internet is like running in the Special Olympics: Even if you win, you're still retarded.[How To Ask Questions|STL Programmer's Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]
yes sure i read/write in binary mode

--=[[TheMatrixXXX]]=--

[edited by - TheMatrixXXX on October 2, 2002 9:23:22 AM]
--=[[TheMatrixXXX]]=--
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]]=--
--=[[TheMatrixXXX]]=--
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 [].
Arguing on the internet is like running in the Special Olympics: Even if you win, you're still retarded.[How To Ask Questions|STL Programmer's Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]
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]]=--
--=[[TheMatrixXXX]]=--

  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;}  
Arguing on the internet is like running in the Special Olympics: Even if you win, you're still retarded.[How To Ask Questions|STL Programmer's Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]
ok thx i will try it.

--=[[TheMatrixXXX]]=--

[edited by - TheMatrixXXX on October 3, 2002 4:50:14 AM]
--=[[TheMatrixXXX]]=--
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]
Arguing on the internet is like running in the Special Olympics: Even if you win, you're still retarded.[How To Ask Questions|STL Programmer's Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]
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]]=--
--=[[TheMatrixXXX]]=--

This topic is closed to new replies.

Advertisement