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.
You can use the
xxd
command with the-i
option to convert any file to a char vector inC
. If you are on Windows you can look into using it in Cygwin.It sounds like you're trying to avoid putting it in a source file, but that's exactly what I'd do:
It's technically possible to keep them as a plain file and write a linker directive file that creates a new data section of the proper size and combines them, but there's really no reason. Put that definition in a separate file and #include it into the file that needs it
You could just generate an array definition. For example, suppose you have
numbers.txt
:I've generated it for the example using:
Then to convert it to C array definition you could use a script:
It produces:
It could be used in your program as follows:
Run it:
Not the solution (this was given before) but: don't put it in a header file. Write a header, which defines a function that returns an array. Then implement this in a .c file. Otherwise, you will end up in a compilation mess...
I agree with the previous answers. The best way is to simply store it in the code and then compile it into the program. For the sake of argument you could look at the format for an executable and add some data/code in there (This is how a lot of viruses work) and simply read from the executable and get the data. http://refspecs.freestandards.org/elf/elf.pdf has the format for an executable. Once again this is for the sake of argument and is not recommended.
You could adapt this solution to numbers: