我有一个我在C++中编译的文件,其中我希望有一个字符串,其值是编译时文件的内容.
换句话说,我想#include文件,但有双引号.
我怎么能用C++做到这一点?
现在如果该文件包含双引号作为其文本的一部分,如何将这些文件转义?
I have a C++ program which I'd like to have output a certain text file at some point. Of course I could have it open, read, and output the text file, but then I have to keep the binary and the text file in sync and located together. I'd rather just compile the file in as a string in some part of my build process.
I've seen things like this done as parts of various IDEs. However I'm building from …
我知道这个网站上有很多类似的问题。我真的很喜欢以下链接中提到的解决方案:
\n\n\n\n经过一些修改,您可以在编译时包含文本文件,例如:
\n\nconstexpr const char* s = \n#include "file.txt"\nRun Code Online (Sandbox Code Playgroud)\n\n但是要使其工作,您必须向原始文件添加字符串文字前缀和后缀,例如
\n\nR"(\nThis is the original content,\nand I don\'t want this file to be modified. but i\n don\'t know how to do it.\n)";\nRun Code Online (Sandbox Code Playgroud)\n\n我的问题是:有没有一种方法可以使其工作但不修改 file.txt?
\n\n(我知道我可以使用命令行工具来制作副本,在副本前面添加和附加到副本,在编译后删除副本。我正在寻找比这更优雅的解决方案。希望不需要其他工具)
\n\n这是我尝试过的(但不起作用):
\n\n#include <iostream>\n\nint main() {\n constexpr const char* s =\n#include "bra.txt" // R"(\n#include "file.txt" //original file without R"( and )";\n#include "ket.txt" // )";\n std::cout << s << "\\n";\n return 0;\n}\n\n/opt/gcc8/bin/g++ -std=c++1z a.cpp\nIn file included from a.cpp:5:\nbra.txt:1:1: …Run Code Online (Sandbox Code Playgroud) 我正在编写一个创建图像文件的程序.我希望在程序的二进制文件中对"原始"图像进行硬编码.
我刚在想...
char image[] = {
#include"image.jpg"
}
Run Code Online (Sandbox Code Playgroud)
但是我需要以某种方式将图像转换为可以作为char数组#included到ac文件中的格式?