将数据保存在.exe文件中 [英] Save data in the .exe file

查看:168
本文介绍了将数据保存在.exe文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以创建一个控制台应用程序来询问输入数据(例如,用户的出生日期,最喜欢的食物,一切),并且我对其进行编程以使其在关闭时将这些数据存储在该.exe中?

Is there a way to create a console application program that asks input data (eg users birth dates, favourite food, everything) and does anything i program it to do and keep those data stored in that .exe when i close it?

这样,当我再次打开它时,所有那些数据仍将保存在那里,所以我只需要更新或修改它们。

This way, when I'll open it again, all those datas will still be saved there, so I just have to update or modify them.

推荐答案

不要在内部保存数据 您的可执行文件,但在其外部



Don't save data inside your executable but outside of it


是否可以创建[。 ..]程序询问输入数据,然后在我关闭它时将那些数据保存在该.exe中?

Is there a way to create a [...] program that asks input data [...] then keep those data stored in that .exe when i close it?

没有简单的方法(但您不需要)。您想要的与持久性应用程序检查点

There is no simple way (but you don't need that). What you want is related to persistence and application checkpointing.

实际上,您可能希望将数据存储在文件-不是您的可执行文件-(可能使用某些文本格式,例如 JSON )或某些数据库(也许和 sqlite ,或与某些 RDBMS 进行交互,例如 PostGreSQL )。对于诸如生日和食物偏爱之类的事情,使用 sqlite 数据库文件可能是个好方法(请参见一些 SQLite教程)。为您的数据库架构做好设计。

In practice, you probably want to store data in a file -not your executable- (perhaps using some textual format like JSON) or in some database (perhaps as simple as some sqlite, or interacting with some RDBMS like PostGreSQL). For things like birthdays and food preference, an sqlite database file is probably the good approach (see some SQLite tutorial). Put efforts in the good design of your database schema.


这样,当我再次打开它时,所有那些数据仍将保存在这里

This way, when I'll open it again, all those datas will still be saved there

如果将它们保存在某些外部文件中(可能是一个简单的 myappdata.sqlite 文件),这些数据仍将存在。您可以轻松地设计程序来创建该文件(如果该文件不存在)(这种情况仅在您第一次运行程序时发生;在下次运行时,您的程序将在启动时从该外部文件中成功读取该数据)。

Those datas will still be there if you keep them in some outside file (perhaps a simple myappdata.sqlite one). You can easily design your program to create that file if it does not exist (this happens only the first time you run your program; on the next runs, your program would successfully read that data from that outside file at startup).

在最新的操作系统(请阅读< a href = http://pages.cs.wisc.edu/~remzi/OSTEP/ rel = nofollow noreferrer>这本教科书以了解有关OS的更多信息),尤其是Windows,MacOSX,Linux,Android, ...,可执行文件应该是只读的。并且它可能在几个 进程中运行同时(在这种情况下,应该怎么办?请考虑 ACID 属性)。

In most current operating systems (read this textbook to learn more about OSes) notably Windows, MacOSX, Linux, Android, ..., the executable is supposed to be read-only. And it might be running in several processes at the same time (in such case, what should happen? Think of ACID properties).

通常的做法是将数据存储在可执行文件的外部 (大多数程序,包括文本处理器,编译器,您的网络浏览器,...正在执行此操作)。您无需解释为什么要在可执行文件内部 中存储一些数据,而这样做是不寻常的,并且是高度特定于操作系统和可执行文件格式的(对于Linux,请仔细研究 elf(5) ...)

The usual practice is to store data outside of the executable (most programs, including your text processor, your compiler, your web browser, ... are doing that). You don't explain why you want to store some data inside the executable, and doing so is unusual and highly operating system specific and executable format specific (for Linux, study carefully elf(5)...)

我建议将数据保存在某些可选文件(或数据库)中-它的文件路径可能具有一些有线常量默认值,等等...。在启动时,请检查是否存在数据(例如,使用POSIX上的 access(2)) ,或者仅通过处理 fopen sqlite3_open 等的失败案例。)。如果不存在,则以某种方式初始化程序数据。在退出时(或节省时间),您可以写入该数据。顺便说一句,大多数程序都是这样做的。

I would suggest to save the data in some optional file (or database) - its filepath could have some wired-in constant default, etc.... At startup, you check the existence of that data (e.g. with access(2) on POSIX, or just by handling the failure case of fopen or sqlite3_open etc....). If it does not exist, you initialize your program data somehow. At exit (or save time), you write that data. BTW most programs are doing so.

请注意,在大多数操作系统和计算机上,软件不仅是单个可执行文件,而且还更多(例如,所需的库和依赖项) ,配置文件,数据文件,构建自动化脚本,例如 Makefile 等)。它的安装是一个公认的技术过程(有时是一个非常复杂的过程),并且包管理器很有帮助。

Notice that on most operating systems and computers, a software is not simply a single executable file, but much more (e.g. required libraries and dependencies, configuration files, data files, build automation scripting such as Makefile, etc...). Its installation is a well identified technical process (sometimes a quite complex one), and package managers are helpful.

我的感觉是,没有特定的动机,您甚至不应该尝试(可变)数据(持久地)存储在可执行文件中(这很复杂) ,因为它非常依赖操作系统和编译器以及构建链,因此非常脆弱,并且异常,并且打开了漏洞 a>)。

My feeling is that without specific motivation, you should not even try to store (mutable) data (persistently) in your executable (it is complex, brittle since very OS & compiler and build-chain specific, unusual, and opens vulnerabilities).

为了完整起见,某些程序实际上通过重写其可执行文件来写入一些数据。在Linux上, GNU emacs 是(实际上,仅在安装过程中)在 unexec.c 文件(非常脆弱,因为特定于OS和编译器),但是该功能为有争议的,而且很可能会消失。

For completeness, some programs did actually write some data by rewriting their executable. On Linux, GNU emacs is doing that (in practice, only during its installation procedure) in its unexec.c file (very brittle, since OS & compiler specific) but that feature is disputed and is likely to disappear.

许多其他系统巧妙地处理了正交持久性: SBCL 具有一些 save-lisp-and-die 原语(通常将状态保存在其他图像文件中)。 Poly / ML 具有一些 export 工具。 J.Pitrat的CAIA系统(请参见本文他的博客; 2016年 tarball 可用-rel = nofollow noreferrer>我的主页)能够完全重新生成其所有C代码和所有必需的数据(成千上万个文件)。 FullPliant 将其状态保存在组织良好的文件树中。这样的持久性或检查点技术与垃圾收集有关(因此,您应该阅读 GC手​​册 ),并且正在使用接近复制垃圾回收的技术和算法。

Many other systems deal cleverly with orthogonal persistence: SBCL has some save-lisp-and-die primitive (it usually persists the state in some other "image" file). Poly/ML has some export facility. J.Pitrat's CAIA system (see this paper and his blog; a 2016 tarball of CAIA is available -with permission- on my home page) is able to regenerate entirely all its C code and all the required data (in thousands of files). FullPliant is persisting its state in a well organized file tree. Such persistence or checkpointing techniques are tied to garbage collection (so you should then read the GC handbook) and are using techniques and algorithms close to copying garbage collection.

FWIW,我当前的项目, bismon ,会正交保留其整个堆,但是要在主要可执行文件之外执行(在理想情况下,我想重新生成所有C或C ++源代码

FWIW, my current project, bismon, is orthogonally persisting its entire heap, but do that outside of the main executable (in an ideal world, I would like to re-generate all the C or C++ source code of it; I am far from that goal).

我的建议是将软件保存在多个文件中:可执行文件,与之相关的C ++源代码,其数据文件(可能还有更多依赖项,即共享库或DLL,字体和图像文件,所需的二进制文件等)。这样,在保持状态时就无需覆盖可执行文件。由于您提到的是C ++(不是 homoiconic ),因此您可以生成您系统的C ++代码(然后称为 Quine 程序)及其持久数据(并将所有生成的C ++的重新编译留给系统的C ++编译器)。我还建议使您的自生成程序成为一些免费软件。 (如果这样做的话,最好编辑问题以提供其URL)。

My recommendation is to keep your software in several files: its executable, the C++ source code related to it, its data files (and probably much more dependencies, i.e. shared libraries or DLLs, font and image files, required binaries, etc...). Then you don't need to overwrite your executable when persisting your state. Since you mention C++ (which is not homoiconic), you could generate the C++ code of your system (then called a Quine program) with its persistent data (and leave the recompilation of all that generated C++ to the system's C++ compiler). I also recommend to make your self-generating program some free software. (if you do that, be nice to edit your question to gives its URL).

在C ++中,您可能会将数据保留在可执行文件中(同样,它是一个不好的想法,我希望能说服您避免这种方法,方法如下:您添加了一个C或C ++源文件(例如 mydata.cc )仅包含数据(例如一些大的 const char data [] = ...多行数据...; )-BTW,<一种href = https://en.wikipedia.org/wiki/X_BitMap rel = nofollow noreferrer> XBM 文件格式可能会令人鼓舞。您保留所有其他 *。o 目标文件(在程序已知的位置)。要保存数据,请在每个保存操作中重新生成 mydata.cc 文件(具有当前状态的新数据),最后运行适当的命令(可能在代码中使用 std :: system )编译该 mydata.cc 并将其与保存的 *。o 链接到一个新的可执行文件中。因此,每个保存操作都需要重新编译 data.cc 并将其与其他 *。o 对象文件(和当然,C ++编译器和链接器(可能还有其他构建自动化工具)将成为程序的必需依赖项。这样的方法并不比保留外部数据文件简单(并且需要保留这些 *。o 对象文件)。

In C++, you might keep the data inside the executable (again, it is a bad idea, and I hope to have convinced you to avoid that approach) in the following way: You add one C or C++ source file (e.g. mydata.cc) which contains only data (e.g. some big const char data[]="... many lines of data ...";) - BTW, the XBM file format could be inspirational. You keep all the other *.o object files (in a place known to your program). To save data, you regenerate that mydata.cc file (with the new data for your current state) at each save operation, and at last you run the appropriate commands (perhaps using std::system in your code) to compile that mydata.cc and link it with the kept *.o into a fresh executable. So every save operation requires the recompilation of data.cc and its linking with other *.o object files (and of course the C++ compiler and linker, perhaps with additional build automation tools, becomes a required dependency of your program). Such an approach is not simpler than keeping an external data file (and requires to keep those *.o object files anyway).


这样,当我再次打开它时,所有那些数据仍将保存在这里

This way, when I'll open it again, all those datas will still be saved there

如果您的目标只是获取过去写入的数据,只需将数据保存在某些可选数据库或文件中(就像许多程序一样:您的文字处理器会要求您输入如果您在没有任何文档的情况下启动它,请在退出之前保存其文档,然后在可执行文件的 outside 中写下一些文字,然后在退出程序之前将其写入。无需覆盖您的可执行文件!

If your goal is just to get the data if it was written in the past, just keep the data in some optional database or file (as many programs do: your word processor would ask you to save its document before exiting if you start it without any document and write a few words in it) outside of your executable and write it before exiting your program. No need to overwrite your executable!

这篇关于将数据保存在.exe文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆