Is it a good idea to include a large text variable in compiled code?

I am writing a program that creates a formatted file for a user, but he not only creates a formatted file, but also does more.

I want to distribute one binary code to the end user, and when the user starts the program, he will generate an XML file for the user with the relevant data.

To achieve this, I want to provide the file contents of a char array variable that is compiled in code. When the user starts the program, I will write out a char file to create an XML file for the user.

char* buffers = "a xml format file contents, \ this represent many block text \ from a file,..."; 

I have two questions.

Q1. Do you have any other ideas on how to compile the contents of my file to a binary, i.e. Distribute it as a single binary.

Q2. Is this even a good idea, as I described above?

+4
source share
6 answers

I recommend using a different file to store data other than entering data into a binary file, unless you have your own reasons. I don’t know of other portable ways to put strings in a binary, but your solution seems to be OK.

However, note that using \ at the end of a line to form lines of several lines, indentation should be taken into account, since they are coupled with begging the following line:

 char* buffers = "a xml format file contents, \ this represent many block text \ from a file,..."; 

Or you can use another form:

 char *buffers = "a xml format file contents," "this represent many block text" "from a file,..."; 
+7
source

What you are describing is certainly the norm for C / C ++. For large amounts of text data or for arbitrary binary data (or even any data that you can save in a file - for example, a zip file), you can write data to a file, directly link it with your program.

An example can be found

+9
source

My answer probably provides a lot of redundant information for the starter theme, but here is what I know about:

  • Embedding in the source code: a simple C / C ++ solution is a bad idea, because every time you want to change your content, you will need:

    • recompilation
    • RELINK

    This may be acceptable, only your content changes very rarely or never if the build time is not a problem (if the application is small).

  • Embedding in a binary file: There are slightly more flexible solutions for embedding content in executable files, but none of them are cross-platform (you did not specify your target platform):

    You will not need to recompile the C ++ file (s), only re-link.

  • Application virtualization: there are special utilities that wrap all your application resources in one executable file, which works the same way as in a virtual machine.

    I know only such utilities for Windows ( ThinApp , BoxedApp ), but probably such things can be for other OSs, or even for cross-platform ones.

  • Consider distributing your application in some form of installer: when you run the installer, it creates all the resources and decompresses the executable file. It is like creating all the material on the main executable. This can be a large and complex package or even a simple self-extracting archive.

Of course, the choice depends on which application you create, who your target audience is, how you send the package to end users, etc. If this is a game, and you focus on children, this is not the same as the Unix console utility for C ++ encoders =)

+3
source

It depends. If you are making a small unified unified utility without the prospect of internationalization, then this is probably great. You do not want to inflate the distribution with a file that no one will ever touch.

But overall, this is bad practice, because ultimately someone may want to change this data, and he or she will have to rebuild it all to fix a typo or something else.

The decision is really up to you.

If you just want to save your distribution in one part, you can also find this topic interesting: Store data in an executable file

+1
source

Why don't you distribute your application with an additional configuration file? for example, package the application executable and configuration file together.

If you want to do this in a single file, try pasting your configuration file into the executable as resources.

0
source

I see more OS than a problem with C / C ++. You can add text to the resource part of your binary / program. In Windows programs, HTML, graphics, and even video files are often compiled into resources that are part of the final binary.

This is convenient for a possible future translation into another language, plus you can modify part of the binary code resource without recompiling the code.

0
source

Source: https://habr.com/ru/post/1489400/


All Articles