Files extraction program

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jun 2009
Posts: 7
Reputation: Joe Shmoe is an unknown quantity at this point 
Solved Threads: 0
Joe Shmoe Joe Shmoe is offline Offline
Newbie Poster

Files extraction program

 
0
  #1
Aug 31st, 2009
I am trying to make a file extraction program in C and I can't get it to work. It compiles ok, but it has a run-time error. I hope you can figure out what I am trying to do here. I don't know if there is a standard way to do this. I am going to make a table with length of files later. Is there a way to determine EOF without having a table of lengths?

Here is the layout of the file:
1. extraction program
2. 4 bytes for number of files in archive
3. table of files (each 16 bytes long)
4. length of each files in table (not implemented)

  1. #include "stdio.h"
  2.  
  3. int main(int argc, char* argv[])
  4. {
  5. //Declair vars
  6. FILE *self;
  7. int fcount = 0;
  8. int i;
  9. //Size of this program (the stub)
  10. const int F_SIZE = 6143;
  11.  
  12. //Open self, set pointer to the end of the stub
  13. self = fopen(argv[0], "rb");
  14. fseek(self, F_SIZE, SEEK_SET);
  15.  
  16. //Read integer for number of files in archive
  17. fread(&fcount, 4, 1, self);
  18. char filenames[fcount][16];
  19.  
  20. //Loop through the file names
  21. for(i = F_SIZE + 4; i < F_SIZE + 4 + (fcount * 16); i+=16)
  22. {
  23. fseek(self, i, SEEK_SET);
  24. fread(&filenames[i][16], 16, 1, self);
  25. }
  26. fclose(self);
  27.  
  28. for(i = 0; i < fcount; i++)
  29. {
  30. self = fopen(&filenames[i][16], "w");
  31. //This part is just to test if it can make the files
  32. fprintf(self, "%c", 144);
  33. fclose(self);
  34. }
  35.  
  36. return 0;
  37. }
Last edited by John A; Aug 31st, 2009 at 11:27 pm. Reason: added code tags
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 251
Reputation: dwks has a spectacular aura about dwks has a spectacular aura about 
Solved Threads: 25
dwks's Avatar
dwks dwks is offline Offline
Posting Whiz in Training

Re: Files extraction program

 
0
  #2
Aug 31st, 2009
  1. char filenames[fcount][16];
You're declaring a variable-length array here. It's not standard C89 to do that. If I were you I'd just declare the array as
  1. char filenames[MAX_FCOUNT][16];

Also:
  1. //Loop through the file names
  2. for(i = F_SIZE + 4; i < F_SIZE + 4 + (fcount * 16); i+=16)
  3. {
  4. fseek(self, i, SEEK_SET);
  5. fread(&filenames[i][16], 16, 1, self);
  6. }
All that seeking is unnecessary. The filenames are all contiguous in the file, so just read the first, then the second, and so on. After reading one filename, your file pointer will already be in the correct position to read the next.

Is there a way to determine EOF without having a table of lengths?
Not really, no.

How are you generating this executable? Because you'll have to add some stuff onto the end to make it work.

I should mention that in doing so, you may mess up the executable format. I'm not very familiar with the windows executable format, but it may require some data at the end of the file (and by adding onto the end of the file, you may be messing it up). A quick way to test this would be to add random junk onto the end of an existing, working program, and see if that crashes it.
dwk

Seek and ye shall find.

"Only those who will risk going too far can possibly find out how far one can go."
-- TS Eliot.

"I have not failed. I've just found 10,000 ways that won't work."
-- Thomas Alva Edison

"The only real mistake is the one from which we learn nothing."
-- John Powell
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 7
Reputation: Joe Shmoe is an unknown quantity at this point 
Solved Threads: 0
Joe Shmoe Joe Shmoe is offline Offline
Newbie Poster

Re: Files extraction program

 
0
  #3
Aug 31st, 2009
You're declaring a variable-length array here. It's not standard C89 to do that. If I were you I'd just declare the array as
Do you mean that I should just make a const called MAX_FCOUNT. If so, what should I set it to?

All that seeking is unnecessary. The filenames are all contiguous in the file, so just read the first, then the second, and so on. After reading one filename, your file pointer will already be in the correct position to read the next.
I didn't know that. I'll correct it.

How are you generating this executable? Because you'll have to add some stuff onto the end to make it work.
For now, I am just going to use copy /b exefile.exe + data.bin + otherstuff.bin archive.exe. That will copy all of the files together. Later on I might make another program to generate it, but I don't need to. Yes, all of the files will be added to the end of the exe.

I should mention that in doing so, you may mess up the executable format.
No, you can add data to the end of an exe without messing it up.
Last edited by Joe Shmoe; Aug 31st, 2009 at 11:19 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC