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?
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?
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.
One good way build an asset file into your c++ executable is to convert it to a header file, which you can include into your source code.
This question has an answer which shows how to use the linux command xxd to convert any arbitrary file into a header file.
script/tool to convert file to C/C++ source code array
If you decide that you need to prevent the more determined user from seeing the text by using tools like hex editors which could view the text inside the executable, you can compress and/or encrypt the file and decrypt it at runtime.
Hope this helps,
James