How do you save data in the program itself?

Is there a way to actually save data in the program itself without having to output the data to a separate text file?
I'm not saying it can't be done, but you cant' easily open and edit a .exe, or overwrite a program that is open, so you will have a data/config file somewhere even if you delete it later.

If you did save data to your program, say at the end, it's going to be in a binary/format and it's not going to be as easy to read.

You could save it in memory, but it's going to be lost if you reboot.

I think the main problem is updating the program while it's running.
You will have to open your .exe, search for the place you want to write your data, insert/replace your data, probably save it as myprogram1.xxx and somehow when you exit, overwrite your program myprogram.exe.

Another issue is if you did manage to overwrite your program with data, your still going to have to reload the program, because the program running in memory isn't the same as on disk anymore.
Last edited on
The short answer no.

When running a program, modern operating systems open an executable file in "protected" mode. This means it can be read, but not written to while it is open for execution.

If you could get around the file being open in protected mode, you would have to deal with the file format. Executable are in a well structured format such as ELF http://flint.cs.yale.edu/cs422/doc/ELF_Format.pdf
Specific file format depends on your operating system. You would have to know how to navigate the file format and find the area you want to modify. It might even mean creating a new section in the file and moving things around in the file.

Topic archived. No new replies allowed.