How can I get the contents of a file at build time into my C++ string?

后端 未结 5 1473
猫巷女王i
猫巷女王i 2021-01-14 00:05

I have a file I am compiling in C++ in which I wish to have a string whose value is the contents of a file at the time of compilation.

In other words, I\'d like to #

5条回答
  •  迷失自我
    2021-01-14 00:42

    You can use xxd -i /tmp/your_file > /tmp/your_file_as_string.c

    Which will define a string literal for you:

    $ cat /tmp/your_file_as_string.c
    unsigned char _tmp_inc[] = {
      0x2e, 0x2f, 0x42, 0x61, 0x74, 0x63, 0x68, 0x2f, 0x6e, 0x65, 0x78, 0x74,
      0x4f, 0x6f, 0x6c, 0x73, 0x2f, 0x73, 0x63, 0x68, 0x65, 0x6d, 0x61, 0x5f,
      ...
      0x74, 0x61, 0x6c, 0x69, 0x64, 0x61, 0x74, 0x6f, 0x72, 0x2e, 0x65, 0x63,
      0x0a
    };
    unsigned int _tmp_inc_len = 1513;
    

    Now you can use that defined string.

提交回复
热议问题