Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hardcoding large data set into executable?

Tags:

c++

I'd like to hardcode large sets of data (integer arrays of varying length, a library of text strings, etc) directly into an executable file, so there are no additional files.

My question is, what is the most practical and organized method for doing this in C++? Where would I place the data, in terms of header or source files? What structure should I use?

I realize this isn't the accepted way of dealing with data. But humour me!

like image 353
Anne Quinn Avatar asked Dec 16 '12 21:12

Anne Quinn


2 Answers

For both C++ and C, you may use header file to put declarations for these variables, and then place actual initialization code into .c (or .cc) file. Both C and C++ have decent initializers syntax. For example:

mydata.h:

extern int x;
extern int a[10];
extern unsigned char *s;

mydata.c:

#include "mydata.h"
int x = 123;
int a[10] = {1,2,3,4,5,6,7,8,9,10};
unsigned char *s = "mystring";

then main.c will have:

#include "mydata.h"
#include <stdio.h>

int main(const int argc, char *argv[])
{
  printf("%d, %d, %s\n", x,a[5],s);
}

test run looks like this:

$gcc -o main main.c mydata.c
$ ./main 
123, 6, mystring

Now, to really get organized, one would write Perl/Python/PHP script to generate such formed files from your datasources, like SQL database of CSV files.

like image 103
vleo Avatar answered Sep 26 '22 15:09

vleo


Try this:

http://sourceforge.net/projects/bin2obj/

Convert your data to an OBJ and link into your project.

like image 24
Pavel Radzivilovsky Avatar answered Sep 26 '22 15:09

Pavel Radzivilovsky