-2

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?

user0042
  • 7,917
  • 3
  • 24
  • 39
  • 1
    Please, provide example of code you wrote and why it's not working. As for your question, I would say that you can indeed include an entire text file within your program. – Fabien Jul 13 '17 at 19:12
  • 1
    Off-topic. You might use operating system specific tricks (file permission, setuid, ...). You could put the text in some sqlite database. You might encrypt it, compress it, or obfuscate it somehow. – Basile Starynkevitch Jul 13 '17 at 19:13
  • 1
    Even if you were to build it in the executable, it would still be readable. Open any .exe you have with notepad. You can see all the strings they have easily – litelite Jul 13 '17 at 19:13
  • 1
    If that's really what you need, zip it & encrypt it. But why would you care if a user can see the file? – George Jul 13 '17 at 19:14
  • How hard do you want the user to have to look to find these assets? Who do you want to keep out on a scale of your grandmother to a government? – user4581301 Jul 13 '17 at 19:19
  • @user4581301 you mean "government to grandmother" right? – Geoff Jul 13 '17 at 19:27
  • Nope. I think we may have confused each other. I'm suggesting a range of low effort involved to high effort. In the general sense, mind you. I've met some bad-ass granny hackers. – user4581301 Jul 13 '17 at 19:34
  • _"Is it possible to build in this text to the executable?"_ What's actually unclear about that question? Of course that is answerable even using only standard c++ techniques. – user0042 Jul 13 '17 at 20:15
  • Probably related question: https://stackoverflow.com/questions/37622767/is-there-a-way-to-pull-in-a-text-resource-into-a-raw-string-literal-using-the-pr – user0042 Jul 13 '17 at 20:23

2 Answers2

0

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.

user0042
  • 7,917
  • 3
  • 24
  • 39
0

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

James Wilson
  • 1,174
  • 1
  • 9
  • 7