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)

#include "stdio.h"

int main(int argc, char* argv[])
{
    //Declair vars
    FILE *self;
    int fcount = 0;
    int i;
    //Size of this program (the stub)
    const int F_SIZE = 6143;
    
    //Open self, set pointer to the end of the stub
    self = fopen(argv[0], "rb");
   	fseek(self, F_SIZE, SEEK_SET);
	
	//Read integer for number of files in archive
    fread(&fcount, 4, 1, self);
    char filenames[fcount][16];
	
	//Loop through the file names
	for(i = F_SIZE + 4; i < F_SIZE + 4 + (fcount * 16); i+=16)
	{
          fseek(self, i, SEEK_SET);
          fread(&filenames[i][16], 16, 1, self);
 }
	fclose(self);
	
	for(i = 0; i < fcount; i++)
	{
          self = fopen(&filenames[i][16], "w");
//This part is just to test if it can make the files
          fprintf(self, "%c", 144);
          fclose(self);
}
	
    return 0;
}

Recommended Answers

All 2 Replies

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

char filenames[MAX_FCOUNT][16];

Also:

//Loop through the file names
for(i = F_SIZE + 4; i < F_SIZE + 4 + (fcount * 16); i+=16)
{
fseek(self, i, SEEK_SET);
fread(&filenames[i][16], 16, 1, self);
}

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.