如何在可执行文件中存储数据
我需要找到一种方法在程序的可执行文件中存储 250 KB 的纯文本数字。
通常,我会将数据放在一个单独的文件中,并让程序在运行时读取它,但这不是这里的选择。相反,程序和数据需要位于一个可执行文件中。
我完全不知道该怎么做(除了写 250.000 #defines :-),我很感激任何建议。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
某种数组怎么样?只需将该定义放入文件中并将其编译到您的程序中即可:
您可以让编译器告诉您外部数据中有多少个元素:
How about an array of some sort. Just put that definition in a file and compile it into your program:
you can have the compiler tell you how many elements are in external data:
您可以只生成一个数组定义。例如,假设您有
numbers.txt
:我已使用以下方法生成了该示例:
然后将其转换为 C 数组定义,您可以使用脚本:
它会生成:
它可以在您的程序如下:
运行:
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:
将其存储为 const 数组:
假设您了解自己的数据集,因此可以针对您的情况得出适当的
NUMBER_MAX_LENGTH
值。当然,您也可以编写一个脚本将数字的平面文件转换为这种格式。如果需要,您甚至可以将数字保存在纯文本数据文件中,并让脚本在构建过程中生成上述相应的 C 代码。
我这样写是因为你说“纯文本数字”,表明你出于某种原因需要它们作为字符串。如果您希望将它们作为整数,那就更简单了:
假设没有一个数字太大而无法存储在 int 中。
Store it as a const array:
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:
Assuming that none of your numbers is too large to store in an int.
您可以将
xxd
命令与-i
选项可将任何文件转换为C
中的字符向量。如果您使用的是 Windows,您可以考虑在 Cygwin 中使用它。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.假设这些数字是常数。假设您可以在“预编译”阶段计算此列表一次。让我们假设有一个函数可以“返回”该列表。
第一阶段:
编写一个调用 getFooNumber() 并完美运行的应用程序。好的。
第二阶段:
获取该函数并将其放入另一个项目中。现在,让我们编写一个小型应用程序,它将生成 250,000 行 C 代码。
这将创建比利奥尼尔谈到的列表。
第三阶段:
使用您刚刚在第 2 阶段创建的头文件,并在第一个项目中使用它从新的 getFooNumber() 返回查找表中的值。
第四阶段:
学习使用Qt,并了解可以直接嵌入文件并使用QFile(":application/numberz.txt")加载它。
笔记:
* C 代码可能已损坏。我没有测试它。
* 如果你使用Windows或Mac,你可能可以对资源系统做类似的事情(MAC也有类似的事情不是吗?)
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.
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?)
我同意前面的答案。最好的方法是简单地将其存储在代码中,然后将其编译到程序中。
为了便于论证,您可以查看可执行文件的格式并在其中添加一些数据/代码(这就是许多病毒的工作方式),然后简单地从可执行文件中读取并获取数据。
http://refspecs.freestandards.org/elf/elf.pdf 的格式对于可执行文件。
再次强调,这是为了争论,并不推荐。
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.
听起来您试图避免将其放入源文件中,但这正是我要做的:
技术上可以将它们保留为普通文件并编写一个链接器指令文件,该文件创建正确的新数据部分大小并将它们组合起来,但确实没有理由。将该定义放在一个单独的文件中,并将其 #include 到需要它的文件中
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 adapt this solution to numbers:
你在什么平台上运行?如果您使用的是 Windows 并且数字不会及时更改,则只需使用资源链接器将文本文件放入程序资源中,然后在代码中读取它即可。
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.
不是解决方案(之前给出过),但是:不要将其放入头文件中。编写一个标头,其中定义一个返回数组的函数。然后在 .c 文件中实现它。否则,你最终会陷入编译混乱的境地......
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...
只需在可执行程序中创建一个包含任意多个字符的字符串,然后让程序的另一部分将其自身作为文件打开,抓取字节,找到您已编译的字符串并直接更改它(确保将那里有一个唯一的字符串,用于用二进制字符串定位实际区域),当重新执行原始程序时,可能需要在执行另一个将数据写入原始程序并重新执行它的程序后关闭程序它可以从二进制文件中声明的字符串中读取新的写入值,并使用它来执行任何任务。
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.