c++include

Compile a program with local file embedded as a string variable?


Question should say it all.

Let's say there's a local file "mydefaultvalues.txt", separated from the main project. In the main project I want to have something like this:

 char * defaultvalues = " ... "; // here should be the contents of mydefaultvalues.txt

And let the compiler swap " ... " with the actual contents of mydefaultvalues.txt. Can this be done? Is there like a compiler directive or something?


Solution

  • One way to achieve compile-time trickery like this is to write a simple script in some interpreted programming language(e.g. Python, Ruby or Perl will do great) which does a simple search and replace. Then just run the script before compiling.

    Define your own #pramga XYZ directive which the script looks for and replaces it with the code that declares the variable with file contents in a string.

    char * defaultvalues = ...
    

    where ... contains the text string read from the given text file. Be sure to compensate for line length, new lines, string formatting characters and other special characters.

    Edit: lvella beat me to it with far superior approach - embrace the tools your environment supplies you. In this case a tool which does string search and replace and feed a file to it.