Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store data inside the executable file

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.

like image 940
michael Avatar asked Apr 11 '10 18:04

michael


People also ask

What is stored in an executable file?

Executable files contain binary machine code that has been compiled from source code. This low-level code instructs a computer's central processing unit on how to run a program. The processor interprets the machine code and tells the computer's hardware what to do.

How do you save an executable file?

In the "File name" text box, type in a name followed by .exe . This will save your file as an EXE. For example, to name the EXE file "bananas", you would type in bananas.exe .

Where executable files are stored?

If the app's EXE file isn't easily available you can browse two locations either C:\Program Files or C:\Program Files (x86) on your system to find the application's main program folder. Then look for the folder with the name that's similar to the publisher of the program. Or the name of the application itself.

What type of data would you find inside an executable file?

An Executable file contains several blobs of data and instructions on how the datas should be loaded into memory. Some of these sections happen to contain machine code that can be executed. Other sections contain program data, resources, relocation information, import information etc.


2 Answers

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);
like image 57
R Samuel Klatchko Avatar answered Oct 27 '22 08:10

R Samuel Klatchko


You could just generate an array definition. For example, suppose you have numbers.txt:

$ head -5 numbers.txt
0.99043748698114
0.0243802034269436
0.887296518349228
0.0644020236531517
0.474582201929554

I've generated it for the example using:

$ perl -E'say rand() for (1..250_000)' >numbers.txt

Then to convert it to C array definition you could use a script:

$ perl -lpE'BEGIN{ say "double data[] = {"; }; 
>     END{ say "};" }; 
>     s/$/,/' > data.h < numbers.txt 

It produces:

$ head -5 data.h
double data[] = {
0.99043748698114,
0.0243802034269436,
0.887296518349228,
0.0644020236531517,

$ tail -5 data.h
0.697015237317363,
0.642250552146166,
0.00577098769553785,
0.249176256744811,
};

It could be used in your program as follows:

#include <stdio.h>    
#include "data.h"

int main(void) {
  // print first and last numbers
  printf("%g %g\n", data[0], data[sizeof(data)/sizeof(*data)-1]);
  return 0;
}

Run it:

$ gcc *.c && ./a.out
0.990437 0.249176
like image 31
jfs Avatar answered Oct 27 '22 08:10

jfs