使用#include指令(预处理器)打印文件的内容
假设我有一个文件 t.txt,其中包含以下两行:
one
two
现在,我想编写一个程序,以某种方式 #include
该文件并打印其内容,仅此而已。也就是说,我希望该文件的内容在编译时作为静态文本出现在我的代码中。
有什么想法吗?
我问的原因是这样的:
我想通过包含我自己的文件(使用 ifndefs
来防止在前两个文件之后递归包含)来创建一个 quine: wikipedia.org/wiki/Quine_(计算)" rel="nofollow noreferrer">http://en.wikipedia.org/wiki/Quine_(计算)。所以我还是很想得到答案。
Say I have a file, t.txt, that contains the following two lines:
one
two
Now, I would like to write a program which will #include
that file somehow and print its contents, nothing more. That is, I want the contents of that file to appear in my code as a static text, at compile time.
Any ideas?
The reason im asking is this:
I would like to create a quine by including my own file (with ifndefs
to prevent recursive inclusion after the first two): http://en.wikipedia.org/wiki/Quine_(computing). So I'd still love to get an answer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
替代解决方案(因为原始解决方案无法无限制地工作,如评论中所述):作为构建过程的一部分,使用脚本(perl 或 python 可以轻松完成)生成
staticstring.h 来自
staticstring.txt
,根据需要添加引号和\n
,然后使用其他解决方案。这样你的原始文件就不会改变。你想改成
我
认为纯粹用预处理器来做是不可能的。
Alternative solution (since the original one won't work without limitations, as mentioned in the comments): As part of your build process, use a script (perl or python would do it easily) to generate
staticstring.h
fromstaticstring.txt
, adding quotes and\n
's as necessary, then use the other solution. This way your original file does not change.You want to change
to
I think that doing it purely with the preprocessor is not possible.
这是 Microsoft 教程:
http://support.microsoft.com/kb/816181/en-us
它没有解释如何以编程方式或通过预处理器嵌入文件,而是通过 Visual Studio 2005+ 菜单嵌入文件。并解释了如何从资源部门读回它。
Borland Delphi 通过预处理器做到这一点,我不了解 C++。但我知道可以手动编辑资源并包含它,就像 VS 所做的那样。
This is the Microsoft Tutorial to do that:
http://support.microsoft.com/kb/816181/en-us
It is not explaining how to embedd the file programatically or via preprocessor, but via the Visual Studio 2005+ menus. And explains how to read it back from the resources sector.
Borland Delphi does that via preprocessor, I don't know about C++. But I know it is possible to edit the resources by hand and include it, just like the VS do.
xxd -i
请参阅此处:之前的回答
xxd -i
See here: previous answer
这样的事情对你有用吗?
example.c
staticstring.txt
我确信有一种方法可以在文本文件中不包含引号的情况下完成此操作,但现在什么也没有想到。
Would something like this work for you?
example.c
staticstring.txt
I'm sure there's a way that you can do it without having the quotes inside the text file, but nothing is coming to mind right now.
您可以在 makefile 中执行此操作:
这是做什么的。
如果您有一个名为 plop.txt 的文本文件。然后它使用变量“char const* plop =”创建一个文件 plop.text.cpp。编译后,临时文件将被删除,但如果您更改 plop.txt 文件,它将自动神奇地重新构建为 plop.text.o
这然后全部组合在一起:
以下示例是我测试的:
You could do this in the makefile:
What this does.
If you have a text file called plop.txt. Then it creates a file plop.text.cpp with the variable 'char const* plop ='. Once compiled the temporary file is removed but if you change the plop.txt file it will be auto-magically be rebuilt into plop.text.o
This is then all combined together:
The following Example is what I tested:
我所做的是这样的:
在我的
Makefile
。然后我将其称为extern const char *COPYING
,但也可以#include
它。What I did was this:
in my
Makefile
. I then referred to it asextern const char *COPYING
, but one can#include
it as well.您可以使用 objcopy 从源文件创建目标文件并将其与实际目标文件链接。它创建符号
_binary_objfile_start、_binary_objfile_end 和 _binary_objfile_size
,您可以将其作为字符数组进行访问。You can use objcopy to create an object file out of the source file and link it with the actual object file. It creates the symbols
_binary_objfile_start, _binary_objfile_end and _binary_objfile_size
, which you can access as an array of char.据我所知,C 中最短的 quine 是这样的:
请注意,这不是有效的 C++,因为它使用
printf()
的隐式定义。要使其成为有效的 C++,您需要执行以下操作:没有任何棘手的包含技巧 - 只需仔细构造一个字符串并将其打印两次,并使用 ASCII 代码(10 和 34 而不是
'\n'
和'"'
) 以避免因必须转义内容而烦恼。As far as I'm aware, the shortest possible quine in C is this:
Note that this is not valid C++, since it uses an implicit definition of
printf()
. To make it valid C++, you need to do this:No tricky include tricks -- just carefully constructing a string and printing it out twice, and using ASCII codes (10 and 34 instead of
'\n'
and'"'
) to avoid annoyingness with having to escape stuff.我不相信这是可能的,因为你不能在 C 中 #include 任意文本文件。原因是 C 中没有多行常量(尽管你可以连接多个单行字符串)常数)。
我能看到的唯一选项是要求引用文本文件的每一行,或者在构建时生成一个临时文件(正如其他人所建议的那样)。
I don't believe it's going to be possible, as you can't #include an arbitrary text file in C. The reason is that there are no multi-line constants in C (although you can concatenate a number of single-line string constants).
The only options I can see are to require that each line of the text file be quoted, or generate a temporary file at build-time (as others have suggested).
tt:
tc
更新:
如果您不是追求纯粹的 quine,而只是追求包含文件内容的东西,您可以执行以下操作:
tc:
tt:
运行此产量
t.t:
t.c
Update:
If you are not after a pure quine, but just something that will spit out the contents of the included file, you can do the following:
t.c:
t.t:
running this yields
这非常简单:
说真的,不可能“#include”任意文件。 (选择其中之一
如果您需要的话,其他答案中给出的预处理建议。)
如果文件的内容不是任意的,而是包含某些内容
对编译器有意义,你也许可以破解一些东西
一起。为了使内容有意义,您可以使用 C 代码和/或
正如我上面所做的那样定义。
It's very easy:
Seriously, it's not possible to "#include" an arbitrary file. (Go with one of
the preprocessing suggestions given in other answers if you need this.)
If the contents of the file is not arbitrary, but contains something
meaningful to the compiler, you might be able to hack something
together. To make the contents meaningful, you can use C code and/or
define's as I did above.
代码:
代码:
代码:
就这样
了在奎因(计算)中看不到任何意义。
多态性是另一个故事;)
玩得开心。
Code:
Code:
Code:
And that's it
Al trough I don't see any point in Quine (computing).
Polymorphism is another story ;)
have fun.