#include一个二进制文件? [英] #include a binary file?

查看:74
本文介绍了#include一个二进制文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我有一种情况需要生成一个新的二进制文件

只需更改几个字节在预先存在的

主模板文件中放置。


我可以逐字节打开并读取原始文件并将其吐出来

a新文件,只需更改相应的字节。但是由于这些

文件只有大约1k字节长,我想将这些字节直接包含在我的代码中,例如:作为静态数组。这将创建一个更清洁的情况,因为我不必知道原始

文件的位置,担心它被删除/移动/修改等等。

代码生成会更容易一些(不是很难)。


然而我不知道你如何将标签附加到

顶部,确保编译器没有重新排列任何字节,

当然,如何使编译器将其视为一块

字面字节被认为是静态数据而不是内联汇编

或者其他什么。


假设该文件包含4字节,FF FD 45 34

我想要的东西相当于:


char template_file [] = {0xFF,0xFD,0X45 ,0x34};


我使用的是Visual C ++版本6.


感谢任何提示。

解决方案

Garyescribió:
让我们说文件包含4个字节,FF FD 45 34

我想要的东西相当于:

char template_file [ ] = {0xFF,0xFD,0X45,0x34};




为什么等效?创造这个。


问候。


>大家好,


我有一种情况,我需要生成一个新的二进制文件,它只是在预先存在的几个地方改变了几个字节
主模板文件。

我可以逐字节打开并读取原始文件,并将其吐出到新文件中,只需更改相应的字节即可。但是,由于这些文件只有大约1k字节长,我想将这些字节直接包含在我的代码中,例如作为静态数组。这将创建一个更清洁的情况,因为我不必知道原始
文件的位置,担心它被删除/移动/修改等等。
代码生成将稍微容易一点(不是很难)。

然而我不清楚如何将标签附加到
顶部,确保编译器没有'''重新排列任何字节,当然,如何使编译器将其视为一个字面的块,将其视为静态数据,而不是内联汇编或其他任何内容。

让我们说这个文件包含4个字节,FF FD 45 34

我想要的东西相当于:

char template_file [] = {0xFF,0xFD,0X45,0x34};

我使用的是Visual C ++版本6.




当我遇到这个问题时,我解决了它通过添加一个汇编模块,我用
使用Nasm来组装

(它有一个名为incbin的指令左右)这样的方法。

然后可以将输出文件与Visual C ++目标文件链接。

你可以从 http://sourceforge.net/projects/nasm


/ Erik


"加里" < FA *********** @ yahoo.com>在消息中写道

news:87 ************************** @ posting.google.c om ...

大家好,


[...]

假设该文件包含4个字节,FF FD 45 34
我想要的东西相当于:

char template_file [] = {0xFF,0xFD,0X45,0x34};

我我正在使用Visual C ++版本6.

感谢您的任何提示。




只需编写一个简单的实用程序来读取二进制文件并生成一个。 cpp
带有上述定义的
文件,然后在你的程序中链接或包含在

中。您可以将程序作为预构建步骤运行,也可以创建

a构建依赖项,这样就不会不必要地运行。


-

Gregg


Hi all,

I have a situation where I need to generate a new binary file which
just changes a couple of bytes in a couple of places in a pre-existing
master template file.

I can open and read the original file byte by byte and spew it out to
a new file, just changing the appropriate bytes. However since these
files are only about 1k bytes long, I''d like to include those bytes
directly into my code, e.g. as a static array. This would create a
cleaner situation in that I wouldn''t have to know where the original
file was, worry about it getting deleted/moved/modified etc. And the
code generation would be a little easier (not that it is hard).

However I am unclear how you would go about attaching a label to the
top of it, making sure that the compiler didn''t rearrange any bytes,
and of course, how to make the compiler treat this as just a block of
literal bytes to be considered static data rather than inline assembly
or whatever.

Let''s say the file contains 4 bytes, FF FD 45 34

I want something that creates the equivalent of:

char template_file[] = {0xFF, 0xFD, 0X45, 0x34 };

I am using Visual C++ version 6.

Thanks for any hints.

解决方案

Gary escribió:

Let''s say the file contains 4 bytes, FF FD 45 34

I want something that creates the equivalent of:

char template_file[] = {0xFF, 0xFD, 0X45, 0x34 };



Why the equivalent? Create just that.

Regards.


> Hi all,


I have a situation where I need to generate a new binary file which
just changes a couple of bytes in a couple of places in a pre-existing
master template file.

I can open and read the original file byte by byte and spew it out to
a new file, just changing the appropriate bytes. However since these
files are only about 1k bytes long, I''d like to include those bytes
directly into my code, e.g. as a static array. This would create a
cleaner situation in that I wouldn''t have to know where the original
file was, worry about it getting deleted/moved/modified etc. And the
code generation would be a little easier (not that it is hard).

However I am unclear how you would go about attaching a label to the
top of it, making sure that the compiler didn''t rearrange any bytes,
and of course, how to make the compiler treat this as just a block of
literal bytes to be considered static data rather than inline assembly
or whatever.

Let''s say the file contains 4 bytes, FF FD 45 34

I want something that creates the equivalent of:

char template_file[] = {0xFF, 0xFD, 0X45, 0x34 };

I am using Visual C++ version 6.



When I had this problem, I solved it by adding an assembly module that I
used Nasm to assemble
(it has a directive called incbin or something like that).
The output file can then be linked with the Visual C++ object files.
You can get nasm from http://sourceforge.net/projects/nasm.

/ Erik


"Gary" <fa***********@yahoo.com> wrote in message
news:87**************************@posting.google.c om...

Hi all,

[...]
Let''s say the file contains 4 bytes, FF FD 45 34

I want something that creates the equivalent of:

char template_file[] = {0xFF, 0xFD, 0X45, 0x34 };

I am using Visual C++ version 6.

Thanks for any hints.



Just write a simple utility that reads the binary file and generates a .cpp
file with the above definition in it, and then link with or include this in
your program. You can run the program as a pre-build step, ore maybe create
a build dependency so it is not run unnecessarily.

--
Gregg


这篇关于#include一个二进制文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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