如何将文件嵌入到可执行文件中? [英] How to embed a file into an executable?

查看:135
本文介绍了如何将文件嵌入到可执行文件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小的演示可执行文件用C ++写成,只依赖于一个5kb PNG图像加载之前,它可以运行,这是用于我做的像素文本。因为这一个文件,我需要给出一个ZIP存档,而不是只有一个可执行文件,这在下载和播放之间产生足够的摩擦,我相信会阻止一些人尝试它。

$ b $我的问题是,有没有反正要嵌入PNG文件(和任何其他文件真正)到可执行文件或源代码,使其是一个单一的文件,可执行文件可以使用它



我可以将PNG解析为字节流,因此不需要转换为像素数据。



提前感谢! (其他类似题目的问题,但他们和他们的答案似乎涉及更具体的问题,并不是很有帮助)



编辑:编译器是Visual C ++ 2010,这是在Windows上(虽然我想避免windows特定的实用程序为此)



edit2 :Alf的答案似乎是最便携的方法,所以我很快写了一个函数来解析PNG文件到一个TXT或头文件,可以读为 unsigned char 数组。它似乎在这种形式与PNG文件本身相同,但我的png加载器将不接受数组。当从内存加载它时,PNG解析器需要一个(void * buffer,size_t length)如果重要。



代码,如果你想看到,但我仍然会接受其他答案,如果你认为他们比这个方法更好:

  void compileImagePNGtoBinary(char * filename,char * output){

FILE * file = fopen(filename,rb);
FILE * out = fopen(output,w);

unsigned char buffer [32];
size_t count;
fprintf(out,#pragma once \\\
\\\
static unsigned char TEXT_PNG_BYTES [] = {);
while(!feof(file)){
count = fread(buffer,1,32,file);

for(int n = 0; n< count; ++ n){
fprintf(out,0x%02X,,buffer [n]);
};
};
fprintf(out,};);
fclose(file);
fclose(out);

};

最终编辑

一种可移植的方式是定义一个函数

  typedef unsigned char Byte; 

Byte const * pngFileData()
{
static Byte const data =
{
//由辅助程序生成的字节数据。
};
return data;
}

然后你所要做的就是写一个小帮手程序, PNG文件作为二进制并生成C ++花括号初始化文本。 修改:@awoodland在对此问题的评论中指出, ImageMagick 有这样一个小助手程序…



当然,而是使用普通的Windows资源方案。



hth。,


I have a small demo executable wrote in C++ that depends only on one 5kb PNG image being loaded before it can run, which is used for a pixel text I made. Because of this one file, I would need to give out a ZIP archive instead of just one executable file, which creates enough friction between download and 'play' that I believe would dissuade some from trying it out.

My question is, is there anyway to embed the PNG file (and any other file really) into the Executable or source code so that it is a single file, and the executable can use it?

I have the ability to parse the PNG as a byte stream, so it does not need converted to pixel data.

Thanks in advance! (Other questions with a similar title to this exist, but they and their answers seem to get into more specific issues and weren't very helpful)

edit:The compiler is Visual C++ 2010 and this is on Windows (though I would want to avoid windows specific utilities for this)

edit2: Alf's answer seemed like the most portable method, so I quickly wrote a function to parse the PNG file into a TXT or header file that could be read as a unsigned char array. It appears to be identical in this form to the PNG file itself, but my png loader won't accept the array. When loading it from memory, the PNG parser takes a (void * buffer, size_t length) if it matters.

The code if you wanted to see, but I'll still accept other answers if you think they're better than this method:

void compileImagePNGtoBinary(char * filename, char * output){

    FILE * file = fopen(filename, "rb");
    FILE * out = fopen(output, "w");

    unsigned char buffer[32];
    size_t count;
    fprintf(out, "#pragma once \n\n static unsigned char TEXT_PNG_BYTES[] = { ");
    while(!feof(file)){
            count = fread(buffer, 1, 32, file);

            for(int n = 0; n < count; ++n){
                    fprintf(out, "0x%02X, ", buffer[n]);
            };
    };
    fprintf(out, "};");
    fclose(file);
    fclose(out);

};

Final Edit: ImageMagick which Alf also mentioned did exactly what I needed of it, thanks!

解决方案

A portable way is to define a function like

typedef unsigned char Byte;

Byte const* pngFileData()
{
    static Byte const data =
    {
        // Byte data generated by a helper program.
    };
    return data;
}

Then all you have to do is to write a little helper program that reads the PNG file as binary and generates the C++ curly braces initializer text. Edit: @awoodland has pointed out in comment to the question, that ImageMagick has such a little helper program…

Of course, for a Windows-specific program, instead use the ordinary Windows resource scheme.

Cheers & hth.,

这篇关于如何将文件嵌入到可执行文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆