Unusual C++ use of #include

Wanted to share with you an unusual way of using the very well-known #include C++ preprocessor directive.

"The #include directive causes a copy of a specified file to be included in place of the directive" – C++ How To Program

The usual use is to write it at the very top of your .h/.cpp files to include other header files like (Windows.h, iostream, cstdio, etc..) to use the what is defined inside in your .h/.cpp file.

The unusual use is to use it to initialize a data-structure like arrays by including a text file that contains the array initialization data between the array initializer list parentheses – e.g XX XXX[] = { <HERE> }. This is is illustrated in the sample program below. The same concept can be used to initialize an array of any dimension.

A Simple Sample

// main.cpp
#include <cstdio>
#include <Windows.h>

struct Person
    char        *pName;
    unsigned    Age;
    unsigned    Height;
    char        Gender;

// Declare and initialize a 1D array of Persons structs using PersonTableData text file
// #include "PersonsTableData" will be expanded at COMPILE TIME to the content of PersonsTableData file
Person PersonsTable[] = {
    #include "PersonsTableData"

int main()
    for (int i = 0; i < _countof(PersonsTable); ++i)
        Person &person = PersonsTable[i];

        printf("%s\t%d\t%d\t%c\n", person.pName, person.Age, person.Height, person.Gender);

    return 0;


PersonsTableData text file content










It is not about how many programming constructs you memorize of your preferred programming language, it is about how can you utilize the constructs you know for your own good.

%d bloggers like this: