Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

c

I am writing a program that produces a formatted file for the user, but it's not only producing the formatted file, it does more.

I want to distribute a single binary to the end user and when the user runs the program, it will generate the xml file for the user with appropriate data.

In order to achieve this, I want to give the file contents to a char array variable that is compiled in code. When the user runs the program, I will write out the char file to generate 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 for how to compile my file contents into binary, i.e, distribute as one binary file.

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

like image 939
gladman Avatar asked Jul 03 '13 04:07

gladman


4 Answers

What you describe is by far the norm for C/C++. For large amounts of text data, or for arbitrary binary data (or indeed any data you can store in a file - e.g. zip file) you can write the data to a file, link it into your program directly.

An example may be found on sites like this one

like image 81
AMADANON Inc. Avatar answered Nov 11 '22 01:11

AMADANON Inc.


I'll recommend using another file to contain data other than putting data into the binary, unless you have your own reasons. I don't know other portable ways to put strings into binary file, but your solution seems OK.

However, note that using \ at the end of line to form strings of multiple lines, the indentation should be taken care of, because they are concatenated from the begging of the next 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,...";
like image 42
Yu Hao Avatar answered Nov 11 '22 01:11

Yu Hao


Probably, my answer provides much redundant information for topic-starter, but here are what I'm aware of:

  1. Embedding in source code: plain C/C++ solution it is a bad idea because each time you will want to change your content, you will need:

    • recompile
    • relink

    It can be acceptable only your content changes very rarely or never of if build time is not an issue (if you app is small).

  2. Embedding in binary: Few little more flexible solutions of embedding content in executables exists, but none of them cross-platform (you've not stated your target platform):

    • Windows: resource files. With most IDEs it is very simple
    • Linux: objcopy.
    • MacOS: Application Bundles. Even more simple than on Windows.

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

  3. Application virtualization: there are special utilities that wraps all your application resources into single executable, that runs it similar to as on virtual machine.

    I'm only aware of such utilities for Windows (ThinApp, BoxedApp), but there are probably such things for other OSes too, or even cross-platform ones.

  4. Consider distributing your application in some form of installer: when starting installer it creates all resources and unpack executable. It is similar to generating whole stuff by main executable. This can be large and complex package or even simple self-extracting archive.

Of course choice, depends on what kind of application you are creating, who are your target auditory, how you will ship package to end-users etc. If it is a game and you targeting children its not the same as Unix console utility for C++ coders =)

like image 3
Ivan Aksamentov - Drop Avatar answered Nov 11 '22 02:11

Ivan Aksamentov - Drop


It depends. If you are doing some small unix style utility with no perspective on internatialization, then it's probably fine. You don't want to bloat a distributive with a file no one would ever touch anyways.

But in general it is a bad practice, because eventually someone might want to modify this data and he or she would have to rebuild the whole thing just to fix a typo or anything.

The decision is really up to you.

If you just want to keep your distributive in one piece, you might also find this thread interesting: Store data in executable

like image 1
akalenuk Avatar answered Nov 11 '22 01:11

akalenuk