2

Is there a platform-independent method to embed file data into a c++ program? For instance, I am making a game, and the levels are stored in text format. But I don't want to distribute those text files with the game itself. What are my options?

rlbond
  • 65,341
  • 56
  • 178
  • 228

3 Answers3

2

This has been asked here before. Basically, you just name the data with an accessible symbol. I like this method best:

You can always write a small program or script to convert your text file into a header > > file and run it as part of your build process.

Community
  • 1
  • 1
Shane C. Mason
  • 7,518
  • 3
  • 26
  • 33
1

You can always put the text in the code. Say, for example, as an array of Strings or array of pointers to characters.

String txt[] = {
  "My first line.\n",
  "My second line.\n"
}

Of course there are better structures in the standard libraries, but in any case you put the text in a source file.

You could also consider putting the text into the package and encrypting it, if you're worried about people accessing it.

Charlie Martin
  • 110,348
  • 25
  • 193
  • 263
1

If the levels are really stored simply as text, you could just declare static char arrays in your source:

static const char level1[] = "abcdeabcdeabcde....";

You can compile and link this right into your application and reference it just like any other variable.

Eric Melski
  • 16,432
  • 3
  • 38
  • 52