How to store data inside the executable file

2019-06-18 00:36发布

I need to find a way to store 250 KB of plain text numbers inside my program's executable file.

Usually, I would put the data in a separate file and let the program read it while it is running, but that's not an option here. Instead, the program and the data need to be in one executable file.

I have absolutely no idea how to do it (except writing 250.000 #defines :-) and I'd appreciate any suggestions.

11条回答
Bombasti
2楼-- · 2019-06-18 01:29

How about an array of some sort. Just put that definition in a file and compile it into your program:

int external_data[] =
{
    ...
};

you can have the compiler tell you how many elements are in external data:

size_t external_data_max_idx = sizeof(external_data) / sizeof(*external_data);
查看更多
Root(大扎)
3楼-- · 2019-06-18 01:31

Lets assume the numbers are constants. Lets assume, that you can compute this list once, in "pre-compilation" stage. Lets assume that there is a function that can "return" that list.

Stage one: write an application that calls getFooNumber() and works perfectly. Nice.

Stage two: Take that function, and put it in another project. Now, lets write a small application that will generate the 250,000 lines of C code.

#include <stdlib>
#define MAX_BLABLA 2500000

int main(int argc, char *argv[] )
{
  FILE *f fopen("fooLookupTable.h");
  long i;
  fprintf( f, "#ifndef FOO_HEADER\n");
  fprintf( f, "#define FOO_HEADER\n");

  fprintf( f, "char [] blabla = {\n\t");
  for( i=0; i<MAX_BLABLA; i ++ )
  {
     fprintf(f, "%d", getFooNumber(i) );
     if (n+1 != MAX_BLABLA)
         fprintf(f, ",");
     if (n%10 == 0)
         fprintf(f, "\n\t");
  }
  fprintf( f, "};\n\n");
  fprintf( f, "#endif // FOO_HEADER\n");
}

This will create the list Billy ONeal talked about.

Stage 3: The use the header file you just created in stage 2, and use it inside the first project to return from the new getFooNumber() the value from the lookup table.

Stage 4: Learn to use Qt, and understand that you can embed the file directly and load it using QFile(":application/numberz.txt").

Notes: * The C code is probably broken. I did not test it. * If you are usign Windows or Mac, you can probably do something similar with the resource system (MAC has a similar thing no?)

查看更多
太酷不给撩
4楼-- · 2019-06-18 01:34

Just make a string of however many characters in your executable program, and then have another section of the program open it's self as a file, grab the bytes, find the string you have compiled and alter it however you want directly (make sure to put a unique string in there for locating the actual area with the string in binary), might need to shut the program down after executing another program which writes the data to the original program and re-executes it, when the original program is re-executed it can read the new written values from the string which was declared in it's binary and use that to perform what ever tasks.

查看更多
太酷不给撩
5楼-- · 2019-06-18 01:39

Store it as a const array:

/* Maximum number of digits in a number, adjust as necessary */
#define NUMBER_MAX_LENGTH 16

/* How many numbers you have (in this case 250K), adjust as necessary */
#define NUMBER_OF_NUMBERS (250 * (1 << 10))

const char data[NUMBER_OF_NUMBERS][NUMBER_MAX_LENGTH+1] =
 { "12345", "2342841", "129131", "18317", /* etc */ };

Presumably you know your data set so you can come up with the appropriate value for NUMBER_MAX_LENGTH in your case.

You can also of course write a script that transforms a flat file of numbers into this format. If you want, you could even keep the numbers in a plain-text data file and have the script generate the corresponding C code as above during your build.

I wrote it that way because you said "plain text numbers", indicating that you need them as strings for some reason. If you'd rather have them as integers, it's even simpler:

/* How many numbers you have (in this case 250K), adjust as necessary */
#define NUMBER_OF_NUMBERS (250 * (1 << 10))

const int data[NUMBER_OF_NUMBERS] =
 { 12345, 2342841, 129131, 18317, /* etc */ };

Assuming that none of your numbers is too large to store in an int.

查看更多
男人必须洒脱
6楼-- · 2019-06-18 01:39

What platform are you running at? If you are on Windows and the numbers won't change in time, then just put your text file to program resources using resource linker, and read it in your code.

查看更多
登录 后发表回答