Search code examples
c++embedded-resource

How can I embed resources within my executable to hide them from a user?


I am building a program that will randomly select text from a file.

What can I use to hide the file with the text from the user?

Is it possible to build in this text to the executable?


Solution

  • Is it possible to build in this text to the executable?

    One way to include a text file as an inlined resource is to modify your textfile a bit and use an #include statement:

    Format of the text file:

    R"rawtext(
    Put all of the text input you want here:
    ...
    )rawtext"
    

    Modification probably could be also done using a little tool that just surrounds the text you want to get inlined with your program at a pre- preprocessing step for compilation:

    At a GNU makefile for example:

     MySource.cpp: MySource.h MyTextFile.inc
    
     MyTextFile.inc: MyTextFile.txt
         sed -e "s/\(.*\)/R'rawtext(\1)rawtext'/" < $< > $@
    

    In your program you can use then:

    std::istrstream input(
    #include "MyTextFile.inc"
    );
    

    And use it with a std::istream like you would do from an externally opened file.

    You can read more about raw string literals here.