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

后端 未结 5 1476
猫巷女王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

    An obvious (if somewhat naive) way is to make a system call and invoke cpp -x c++ file.cpp, and then read the contents of the stream directly into a string just like you would any other file-read operation.

    This invokes the C preprocessor on file.cpp in C++ mode, which is exactly identical to running #include ... on it. This value will then be stored in the string object.

提交回复
热议问题