|
Author |
Message |
|
|
Posted Dec 02, 2008 at 2:59:31 AM
Subject: Creating binary file formats in C
I've been learning C for a while. I've taken a lot of interest in manipulating binary data with C. I was hoping someone could point out any web pages that give an introduction to creating binary file formats. I've been reading one book that talks a little bit about 'magic bytes' and binary structures, but the book is very old and I was hoping for some tips and basic guidelines from another source. I've got a basic understanding of how to read/write to binary files.
Any personal tips from the C gurus on this forum would also be appreciated.
PerlCoder (http://indicium.us)
|
tophandcwby
Joined Apr 10, 2008 Posts: 81
Other Topics
|
Posted:
Dec 06, 2008 12:59:11 PM
Subject: Creating binary file formats in C
I'm not quite sure of what you are asking about.
All files are binary. Some are encoded with ascii. The binary byte
0100 0000 is hex 40 or the character 'A' in ascii. You can create and read a binary file using most of the file manipulating functions and calls.
To become more familiar with what is actually in a file, you can use a binary editor such as khexedit. Or just try
$ od -t x1 filename | less
This will give you a hex dump in byte size chunks.
As far as magic bytes, have a look at the man page for file
$ man file
|
nodan
Joined Jan 09, 2009 Posts: 1
Other Topics
|
Posted:
Jan 09, 2009 10:23:05 AM
Subject: Creating binary file formats in C
Don't.
Don't use binary (i.e. not readable in a text editor) file formats. Use xml instead.
|
Rubberman
Joined Jul 30, 2007 Posts: 944
Location:40 miles west of Chicago
Other Topics
|
Posted:
Jan 10, 2009 3:46:44 AM
Subject: Creating binary file formats in C
There are times when a binary data file is a good solution to some types of problems of data storage. As some of the other posters have pointed out, you cannot read them with normal tools, and you will either have to provide such if you want to be able to read the file aside from the application that manipulates it, or provide complete documentation so someone else can do this.
That said, you need to decide whether or not this data will be accessible by systems other than the platform it was written on (word size - 32bit vs 64bit, endian-ness of the CPU, programming language, etc). If so, then you will need to use network byte order encoding/decoding for all non-character data and deal with structure padding issues as well. Since you are learning C, this is a good exercise to help you start getting comfortable with some of the many gotchas you will encounter doing low-level programming. After many years of real-time and embedded software design and development, I have come to appreciate the strengths of a good human-readable wire format such as Tk/Tcl and XML for external data storage, even though it is not "cheap" from the storage and processor overhead perspectives. There are times and situations when binary data storage is necessary and early in your career is a good time to learn how to do it. Good luck!
Sometimes real fast is almost as good as real time.
Remember, Google is your friend!
|