Search code examples
c++includecharstringify

assignfile contents via #include to char array


I would like to do the following in C++:

static const char* data[] = { #include "mydata.hh" };

where the contents of mydata.hh are supposed to be assign as a char array to data. I tried already various variants and (one and two level) stringification, via #x, of the #include . None worked so far. Is this actually possible or did I miss something?

Thanks ahead.


Solution

  • You can:

    static const char* data[] = {  // <--- new line 
    #include "mydata.hh" 
    };   // <--- don't forget semi-colon
    

    But why?

    This is all assuming mydata.hh contains something compatible with the declaration:

    //mydata.hh
    "bla", "goo", "foo"