C/C++,你能#include 一个文件到一个字符串文字中吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1246301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:18:02  来源:igfitidea点击:

C/C++, can you #include a file into a string literal?

c++cincludec-preprocessorstring-literals

提问by Joseph Garvin

I have a C++ source file and a Python source file. I'd like the C++ source file to be able to use the contents of the Python source file as a big string literal. I could do something like this:

我有一个 C++ 源文件和一个 Python 源文件。我希望 C++ 源文件能够将 Python 源文件的内容用作大字符串文字。我可以做这样的事情:

char* python_code = "
#include "script.py"
"

But that won't work because there need to be \'s at the end of each line. I could manually copy and paste in the contents of the Python code and surround each line with quotes and a terminating \n, but that's ugly. Even though the python source is going to effectively be compiled into my C++ app, I'd like to keep it in a separate file because it's more organized and works better with editors (emacs isn't smart enough to recognize that a Cstring literal is python code and switch to python mode while you're inside it).

但这行不通,因为每行末尾都需要有 \'s。我可以手动复制并粘贴 Python 代码的内容,并用引号和终止 \n 将每一行括起来,但这很难看。即使 python 源代码将被有效地编译到我的 C++ 应用程序中,我还是想将它保存在一个单独的文件中,因为它更有条理并且与编辑器配合得更好(emacs 不够聪明,无法识别C字符串文字是 python 代码,当你在里面时切换到 python 模式)。

Please don't suggest I use PyRun_File, that's what I'm trying to avoid in the first place ;)

请不要建议我使用 PyRun_File,这是我首先要避免的;)

采纳答案by bdonlan

The C/C++ preprocessor acts in units of tokens, and a string literal is a singletoken. As such, you can't intervene in the middle of a string literal like that.

C/C++ 预处理器以标记为单位起作用,字符串文字是单个标记。因此,您不能像那样干预字符串文字的中间。

You could preprocess script.py into something like:

您可以将 script.py 预处理为类似以下内容:

"some code\n"
"some more code that will be appended\n"

and #include that, however. Or you can use xxd? -ito generate a C static array ready for inclusion.

和#include,但是。或者您可以使用它来生成准备包含的 C 静态数组。xxd? -i

回答by Michael Bishop

This won't get you all the way there, but it will get you pretty damn close.

这不会让你一路走到那里,但它会让你非常接近。

Assuming script.pycontains this:

假设script.py包含这个:

print "The current CPU time in seconds is: ", time.clock()

First, wrap it up like this:

首先,像这样包装它:

STRINGIFY(print "The current CPU time in seconds is: ", time.clock())

Then, just before you include it, do this:

然后,就在您包含它之前,请执行以下操作:

#define STRINGIFY(x) #x

const char * script_py =
#include "script.py"
;

There's probably an even tighter answer than that, but I'm still searching.

可能有比这更严格的答案,但我仍在寻找。

回答by Michael Burr

The best way to do something like this is to include the file as a resource if your environment/toolset has that capability.

如果您的环境/工具集具有该功能,那么执行此类操作的最佳方法是将该文件作为资源包含在内。

If not (like embedded systems, etc.), you can use a bin2c utility (something like http://stud3.tuwien.ac.at/~e0025274/bin2c/bin2c.c). It'll take a file's binary representation and spit out a C source file that includes an array of bytes initialized to that data. You might need to do some tweaking of the tool or the output file if you want the array to be '\0' terminated.

如果不是(如嵌入式系统等),您可以使用 bin2c 实用程序(类似于http://stud3.tuwien.ac.at/~e0025274/bin2c/bin2c.c)。它将采用文件的二进制表示形式并输出包含初始化为该数据的字节数组的 C 源文件。如果您希望数组以 '\0' 终止,您可能需要对工具或输出文件进行一些调整。

Incorporate running the bin2c utility into your makefile (or as a pre-build step of whatever you're using to drive your builds). Then just have the file compiled and linked with your application and you have your string (or whatever other image of the file) sitting in a chunk of memory represented by the array.

将运行 bin2c 实用程序合并到您的 makefile 中(或作为您用于驱动构建的任何内容的预构建步骤)。然后只需将文件编译并与您的应用程序链接,您的字符串(或文件的任何其他图像)就位于由数组表示的内存块中。

If you're including a text file as string, one thing you should be aware of is that the line endings might not match what functions expect - this might be another thing you'd want to add to the bin2c utility or you'll want to make sure your code handles whatever line endings are in the file properly. Maybe modify the bin2c utility to have a '-s' switch that indicates you want a text file incorportated as a string so line endings will be normalized and a zero byte will be at the end of the array.

如果您将文本文件作为字符串包含在内,您应该注意的一件事是行尾可能与函数期望的不匹配 - 这可能是您想要添加到 bin2c 实用程序的另一件事,或者您会想要确保您的代码正确处理文件中的任何行结尾。也许修改 bin2c 实用程序以具有一个“-s”开关,该开关指示您希望将文本文件合并为字符串,以便行尾将被规范化,并且零字节将位于数组的末尾。

回答by Steve Jessop

You're going to have to do some of your own processing on the Python code, to deal with any double-quotes, backslashes, trigraphs, and possibly other things, that appear in it. You can at the same time turn newlines into \n (or backslash-escape them) and add the double-quotes on either end. The result will be a header file generated from the Python source file, which you can then #include. Use your build process to automate this, so that you can still edit the Python source as Python.

您将不得不对 Python 代码进行一些自己的处理,以处理其中出现的任何双引号、反斜杠、三合字母以及可能的其他内容。您可以同时将换行符转换为 \n (或反斜杠转义它们)并在任一端添加双引号。结果将是从 Python 源文件生成的头文件,然后您可以#include。使用您的构建过程自动执行此操作,以便您仍然可以将 Python 源代码编辑为 Python。

回答by Brian

You could use Cogas part of your build process (to do the preprocessing and to embed the code). I admit that the result of this is probably not ideal, since then you end up seeing the code in both places. But any time I see the "Python," "C++", and "Preprocessor" in closs proximity, I feel it deserves a mention.

您可以将Cog用作构建过程的一部分(进行预处理和嵌入代码)。我承认这样做的结果可能并不理想,因为您最终会在两个地方看到代码。但每当我近距离看到“Python”、“C++”和“预处理器”时,我都觉得它值得一提。

回答by Val

Here is how automate the conversion with cmd.exe

这是使用 cmd.exe 自动转换的方法

------ html2h.bat ------

------ html2h.bat ------

@echo off
echo const char * html_page = "\
sed "/.*/ s/$/ \n\/" ../src/page.html | sed s/\"/\\x22/g 
echo.
echo ";

It was called like

它被称为

cmd /c "..\Debug\html2h.bat" > "..\debug\src\html.h"

and attached to the code by

并附加到代码

#include "../Debug/src/html.h"
printf("%s\n", html_page);

This is quite system-dependent approach but, as most of the people, I disliked the hex dump.

这是非常依赖于系统的方法,但作为大多数人,我不喜欢十六进制转储。

回答by AareP

Use fopen, getline, and fclose.

使用fopengetlinefclose