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

发布于 2024-07-30 00:42:13 字数 428 浏览 6 评论 0原文

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

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

但这是行不通的,因为每行末尾都需要有 \ 。 我可以手动复制并粘贴 Python 代码的内容,并用引号和终止 \n 括住每一行,但这很丑陋。 尽管 python 源代码将有效地编译到我的 C++ 应用程序中,但我想将其保存在一个单独的文件中,因为它更有组织性并且与编辑器配合得更好(emacs 不够聪明,无法识别 C 字符串文字是 python 代码,当您在其中时切换到 python 模式)。

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

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:

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 C string literal is python code and switch to python mode while you're inside it).

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

萌︼了一个春 2024-08-06 00:42:13

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

不过,您可以将 script.py 预处理为类似:

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

和 #include 的内容。 或者您可以使用 xxd-i 生成一个可供包含的 C 静态数组。

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

You could preprocess script.py into something like:

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

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

活雷疯 2024-08-06 00:42:13

这不会让你一路到达目的地,但它会让你非常接近。

假设 script.py 包含以下内容:

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

首先,像这样包装它:

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

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

#define STRINGIFY(x) #x

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

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

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

Assuming script.py contains this:

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.

故乡的云 2024-08-06 00:42:13

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

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

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

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

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.

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.

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.

腻橙味 2024-08-06 00:42:13

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

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.

笙痞 2024-08-06 00:42:13

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

You could use Cog as 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.

东走西顾 2024-08-06 00:42:13

这是如何使用 cmd.exe 自动进行转换

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

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

它被称为类似

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

并附加到代码中

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

这是完全依赖于系统的方法,但是,正如大多数人一样,我不喜欢十六进制转储。

Here is how automate the conversion with cmd.exe

------ 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.

因为看清所以看轻 2024-08-06 00:42:13

使用fopengetlinefclose

Use fopen, getline, and fclose.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文