943,813 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 388
  • C RSS
Aug 31st, 2009
0

Files extraction program

Expand Post »
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
Similar Threads
Reputation Points: 43
Solved Threads: 0
Light Poster
Joe Shmoe is offline Offline
36 posts
since Jun 2009
Aug 31st, 2009
0

Re: Files extraction program

  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.

Quote ...
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.
Reputation Points: 185
Solved Threads: 28
Posting Whiz in Training
dwks is offline Offline
269 posts
since Nov 2005
Aug 31st, 2009
0

Re: Files extraction program

Quote ...
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?

Quote ...
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.

Quote ...
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.

Quote ...
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.
Reputation Points: 43
Solved Threads: 0
Light Poster
Joe Shmoe is offline Offline
36 posts
since Jun 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: comparing string in array
Next Thread in C Forum Timeline: implicit declaration?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC