相关疑难解决方法(0)

如何在构建时将文件的内容放入我的C++字符串中?

我有一个我在C++中编译的文件,其中我希望有一个字符串,其值是编译时文件的内容.

换句话说,我想#include文件,但有双引号.

我怎么能用C++做到这一点?

现在如果该文件包含双引号作为其文本的一部分,如何将这些文件转义?

c++ string include

4
推荐指数
1
解决办法
1836
查看次数

如何将文本文件编译为 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 …

c++ gcc clang

3
推荐指数
1
解决办法
2086
查看次数

如何在编译时将文本文件作为字符串包含在内,而不在文本文件中添加 c++11 字符串文字前缀和后缀

我知道这个网站上有很多类似的问题。我真的很喜欢以下链接中提到的解决方案:

\n\n

/sf/answers/1751506431/

\n\n

经过一些修改,您可以在编译时包含文本文件,例如:

\n\n
constexpr const char* s = \n#include "file.txt"\n
Run Code Online (Sandbox Code Playgroud)\n\n

但是要使其工作,您必须向原始文件添加字符串文字前缀和后缀,例如

\n\n
R"(\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)";\n
Run 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)

metaprogramming c++11

2
推荐指数
1
解决办法
2154
查看次数

#include ansi C中的图像

我正在编写一个创建图像文件的程序.我希望在程序的二进制文件中对"原始"图像进行硬编码.

我刚在想...

char image[] = {
#include"image.jpg"
}
Run Code Online (Sandbox Code Playgroud)

但是我需要以某种方式将图像转换为可以作为char数组#included到ac文件中的格式?

c image-file c-preprocessor

1
推荐指数
1
解决办法
1493
查看次数

标签 统计

c++ ×2

c ×1

c++11 ×1

c-preprocessor ×1

clang ×1

gcc ×1

image-file ×1

include ×1

metaprogramming ×1

string ×1