6

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?

Alex B
  • 24,678
  • 14
  • 64
  • 87
gladman
  • 1,208
  • 4
  • 19
  • 39
  • If you want to put it in `.c` rather than `.h`. If its in `.h` compiler may add it to object file of each `.c` in which its included increasing the resultant binary size many fold. – Rohan Jul 03 '13 at 04:54
  • 1
    Well, making a `char *` point to non-modifiable memory is not a good start. In C++11, you also have raw string literals to make it neater. – chris Jul 03 '13 at 05:05
  • With C++11, you can use multiline raw string literals too: see e.g. emsr's answer at http://stackoverflow.com/questions/1135841/c-multiline-string-literal – Tony Delroy Jul 03 '13 at 05:07
  • Taking a step back: Does it need to be text, or can you eliminate (part of) it? So, could you generate (part of) this XML from binary data, with program logic, or from an external source? – meaning-matters Jul 03 '13 at 05:39
  • -1. Author has resisted all attempts to get more information about the framework and the Operating System that he/she is using. In the end, the answer accepted has more to do with formatting C/C++ strings so that unwanted whitespace on the left doesn't get into the output, than with the original question. – Tony Jul 04 '13 at 05:58

6 Answers6

9

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

AMADANON Inc.
  • 5,753
  • 21
  • 31
  • AMADANON Inc. I can't open the site link you had gave to me. – gladman Jul 03 '13 at 05:26
  • It works fine for me. Try searching for `linking a binary blob with gcc` - my link is the first search from google, but probably any one of the top 4 give the same information – AMADANON Inc. Jul 03 '13 at 22:20
7

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,...";
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
3

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):

    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 =)

Ivan Aksamentov - Drop
  • 12,860
  • 3
  • 34
  • 61
  • time to recompile and link is neglectable, deploying to endusers is much more time consuming. 2. resource files are linked as well, kind of "contradictory" to your 1st argument. 3 and 4 takes .. time to build the installer or the virtual app (i have done these kinds of deployments with help of nsis for example) ... which is also kind of "contradictory" to your first argument. the only argument against "resource as part of the binary" is the amount of bytes the payload to the binary has. – akira Jul 03 '13 at 06:59
1

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

Community
  • 1
  • 1
akalenuk
  • 3,815
  • 4
  • 34
  • 56
0

Why don't you distribute your application with an additional configuration file? e.g. package your application executable and config file together.

If you do want to make it into a single file, try embed your config file into the executable one as resources.

Miles Chen
  • 793
  • 1
  • 10
  • 20
0

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

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

Tony
  • 1,566
  • 2
  • 15
  • 27
  • It's C/C++ language, how to generate resource binary? – gladman Jul 03 '13 at 05:29
  • It depends on the Operating System that you are using. It would be the same if you were using Java or C#. – Tony Jul 03 '13 at 05:32
  • GUI frameworks on Windows, Mac and Linux, all have different ways to add resources. But from your reaction I guess it is a non-GUI program, to be run from command console/terminal. In that case it is Ok to have it in code, as long as you don't need text changes without re-compilation of the whole program. – Tony Jul 03 '13 at 06:35