473,398 Members | 2,113 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

#include a binary file?

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.
Jul 19 '05 #1
13 11973
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.
Jul 19 '05 #2
> 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
Jul 19 '05 #3
"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

Jul 19 '05 #4
Julián Albo <JU********@terra.es> wrote in message news:<3F***************@terra.es>...
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.


Because the file is actually 1000 bytes long?
Jul 19 '05 #5
Gary wrote:
Julián Albo <JU********@terra.es> wrote in message news:<3F***************@terra.es>...
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.


Because the file is actually 1000 bytes long?


I like Gregg's solution. An alternative would be something like this:

char template_file[] = {

#include "bytes.dat"

};

But the file has to be in the expected format. Therefore, if it's just a
binary file you'll have to convert it first. Basically you end up with a
less elegant version of Gregg's solution, but it's possible that it
could be better suited to your particular problem.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #6
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.

Because the file is actually 1000 bytes long?


Can't you write a program that do the conversion for you?

Regards.
Jul 19 '05 #7


Gary wrote:

Julián Albo <JU********@terra.es> wrote in message news:<3F***************@terra.es>...
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.


Because the file is actually 1000 bytes long?


A perfect task for a program to do it.
You are a programmer, aren't you?

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #8
Karl Heinz Buchegger <kb******@gascad.at> wrote in message news:<3F***************@gascad.at>...
A perfect task for a program to do it.
You are a programmer, aren't you?


Hmmmm.... I write file-conversion programs to suit very specific needs
I have for transferring data between various audio programs on the
order of once every three years. So I don't really consider myself a
"programmer". I am just trying to get over this particular hump at
which point I may never write another program, unless the need arises.

I certainly don't get *paid* to do it. Between 20 and 10 years ago I
did get paid to write C and assembler for embedded systems. I taught
myself Windows API (pre-MFC) and C++ by reading books and using the
compilers but never had a job doing this, nor did I study programming
in college beyond mandatory BASIC (gasp) FORTRAN (erkk) and assembler
(SHL A). Still I find myself most comfortable writing console type
apps in C... but can just barely manage to do Windows apps using MFC
in C++. If I hadn't found the code to an MFC program I wrote 3 years
ago I would be writing a console app!

Anyway I thought this NG would be a good place to ask, and I got the
answer I needed. C++ has so many things that I'm not familiar with
that I thought this might be one of them. A program to read a binary
file and print each byte followed by a comma will be trivial and I am
not even going to worry about trimming the last comma. I'll just do
it by hand!

Thanks to all for the helpful responses.
Jul 19 '05 #9
"Gary" <fa***********@yahoo.com> wrote in message
news:87**************************@posting.google.c om...

[...]
that I thought this might be one of them. A program to read a binary
file and print each byte followed by a comma will be trivial and I am
not even going to worry about trimming the last comma. I'll just do
it by hand!


You don't have to. It is valid to leave the trailing comma. E.g.,

char data[] = { 1, 2, 3, };

is valid.
Jul 19 '05 #10
"Gregg" <gr***********@hotmail.com> wrote in message news:<aR*******************@newsread1.news.atl.ear thlink.net>...

You don't have to. It is valid to leave the trailing comma. E.g.,

char data[] = { 1, 2, 3, };


Yeah, but it's kinda like a "dangling chad" to me. Just personal preference.
Jul 19 '05 #11
OK, at the risk of drawing ire, here's my "C" code entry in solution
of this problem. Can it be made more straightforward in C++?

#include "stdio.h"

int main(int argc, char **argv)
{
char c;
FILE *binfile;
int ii,i;

if(argc != 2)
return(-1);

binfile = fopen(argv[1], "rb");

if(binfile != NULL)
{
printf("char %s[] = \n{\n",argv[1]);
i = 0;
while(!feof(binfile))
{
if((i % 88) == 0)
{
printf("\n/* ");
for(ii = 0; ii < 8; ii++)
printf("[%4x]",i + ii);
printf("*/");
}
if((i % 8) == 0)
printf("\n/*[%4x]*/ ",i);
i++;
c = getc(binfile);
printf("0x%2.2x, ", c & 0xff);
}
printf("\n};\n");
return i;
}
else
return 0;
}
Jul 19 '05 #12


Gary wrote:

OK, at the risk of drawing ire, here's my "C" code entry in solution
of this problem. Can it be made more straightforward in C++?


Well. At least it can be made correct in C :-)

feof returns true only until you have tried AND failed to read past
the end of file.

Thus whenever you see a loop:

while( !feof( ... ) {

// read from file
// do something with the thing read
}

you know immediatly that this will not work correctly
in all circumstances (usually the last thing read from the file
will be processed twice)

feof is meant to be used *after* the reading loop has terminated
to figure out *why* the loop has termineted. If it was because of
eof, then everything is correct, the file has been read completely.

Note that getc (as well as fgetc) returns an int and not a char!!!! This is
important because getc and fgetc need a way to tell the caller: EOF reached
or some other error condition, stop reading from the file.

The following uses this. It is based on what you had:

#include <stdio.h>
#include <stdlib.h>

int main( int argc, char *argv[] )
{
int Byte;
FILE* BinFile;
int ii,i;

if( argc != 2 )
{
printf( "Usage: DumpBin <FileName>\n" );
return EXIT_FAILURE;
}

BinFile = fopen( argv[1], "rb" );

if( !BinFile )
{
printf( "Error: Failed to open file: '%s'\n", argv[1] );
return EXIT_FAILURE;
}

printf( "char %s[] = \n{\n", argv[1] );
i = 0;

while( ( Byte = fgetc( BinFile ) ) != EOF )
{
if( (i % 88) == 0 )
{
printf("\n/* ");
for(ii = 0; ii < 8; ii++)
printf("[%04x]",i + ii);
printf("*/");
}
if((i % 8) == 0)
printf("\n/*[%04x]*/ ",i);
i++;

printf( "0x%02x, ", Byte );
}

if( !feof( BinFile ) )
{
printf( "Error: Reding from file: '%s'\n", argv[1] );
printf( " Could read %d bytes before failure\n", i );
fclose( BinFile );
return EXIT_FAILURE;
}

printf("\n};\n");
fclose( BinFile );

return EXIT_SUCCESS;
}

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #13
Karl Heinz Buchegger <kb******@gascad.at> wrote in message news:<3F***************@gascad.at>...
Gary wrote:

OK, at the risk of drawing ire, here's my "C" code entry in solution
of this problem. Can it be made more straightforward in C++?


Well. At least it can be made correct in C :-)


Thanks! I need all the help I can get. Old habits die hard.
Jul 19 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: J. Campbell | last post by:
OK...I'm in the process of learning C++. In my old (non-portable) programming days, I made use of binary files a lot...not worrying about endian issues. I'm starting to understand why C++ makes...
6
by: news.hku.hk | last post by:
i am writing a small program to get a binary file, but i really don't know how to convert the strings in a buffer to the required binary bytes, most probably i can't read each bytes in buffer...
7
by: spike | last post by:
Im writing a program to search for a string in a binary file. And it works. The problem is: It is sooo slow! how can i make it faster? It takes 27 seconds just to search a 5 meg file. I guess it...
4
by: pedagani | last post by:
I want to copy only a part of the binary file delimited by offset values say START_OFFSET_ & END_OFFSET_ which can be as huge as a 50 Giga. Below is the program where I could set a START_OFFSET_...
1
by: vinothg | last post by:
Hi , I have a binary file which contains 30,000 strings of 20 bytes each.I need to search for a string in the file to see whether the particular string exists. The sample code which i wrote...
9
by: Use*n*x | last post by:
Hello, I have a binary file (image file) and am reading 4-bytes at a time. The File size is 63,480,320 bytes. My assumption is that if I loop through this file reading 4 bytes at a time, I...
2
by: jhansi | last post by:
create a binary file and insert the records and query on those inserted record.. queries are: query based on search by name,search by age and search by designation. the problem is i have...
16
by: vizzz | last post by:
Hi there, i need to find an hex pattern like 0x650A1010 in a binary file. i can make a small algorithm that fetch all the file for the match, but this file is huge, and i'm scared about...
22
by: xiao | last post by:
Can I fullfill this task? Using fred and fwrite?
12
by: freeseif | last post by:
Hi programmers, I want read line by line a Unicode (UTF-8) text file created by Notepad, i don't want display the Unicode string in the screen, i want just read and compare the strings!. This...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.