    <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/">
     <channel>
        <title>Linux.com :: Creating binary file formats in C</title>
        <link>http://www.linux.com/archive/forums/topic/3561</link>
        <description>Everything Linux and Open Source</description>
        <dc:language>en-us</dc:language> 
        <dc:creator>Linux.com Admin</dc:creator> 
        <admin:generatorAgent rdf:resource="http://www.linux.com" /> 
        <admin:errorReportsTo rdf:resource="mailto:noreply@linux.com" />
       <sy:updatePeriod>hourly</sy:updatePeriod>
       <sy:updateFrequency>1</sy:updateFrequency>
       <docs>http://backend.userland.com/rss</docs>



<div class="xarbb-noteswrapper">
</div>

   <div class="xarbb-noteswrapper">
       <div class="xar-sub rightnote">
           <div>
                    <a href="http://www.linux.com/archive/forums/topic/3933">
Previous Topic</a>
|
                    <a href="http://www.linux.com/archive/forums/topic/2734">
Next Topic</a>
</div>

           <div>
                    <a href="http://www.linux.com/archive/forums/printtopic/3561?theme=print">
Print this topic</a> |
You are not subscribed            </div>
        </div>
        <div style="clear: both;"></div>
    </div>

    <h1>
        <a class="xar-title" href="http://www.linux.com/archive/forums/topic/3561">Creating binary file formats in C</a>
   </h1>


   <div class="xarbb-topiccontrols">
<form action="http://www.linux.com/archive/forums?func=jump" method="post">
   <label for="xarbb_jump_menu">Forum Jump</label>
    <select name="f" id="xarbb_jump_menu" onchange="this.form.submit();">
        <optgroup label="Options">
            <option value="5" selected="selected">
Please select forum            </option>
            <option value="5">--------------------</option>
        </optgroup>
        <optgroup label="Forums">
                <option value="1">New to Linux</option>
                <option value="2">Hardware</option>
                <option value="3">Linux Distributions</option>
                <option value="4">Applications</option>
                <option value="5">Programming and Development</option>
                <option value="6">Miscellaneous Discussion</option>
                <option value="7">Linux.com Writers Forum</option>
                <option value="8">Jobs</option>
        </optgroup>
    </select>
    <input type="submit" class="button" value="Jump" />
</form>
<div style="clear: both;"></div>
   </div>

   <div class="xarbb-topiccontrols">

<span class="xar-sub">
    <a href="http://www.linux.com/archive/forums">
Forum Index    </a>
    <span style="font-size: larger;">&#187;</span>
    <a href="http://www.linux.com/archive/forums/category/4181">
        Forums 
    </a>
    <span style="font-size: larger;">&#187;</span>
    <a href="http://www.linux.com/archive/forums/forum/5">
        Programming and Development   </a>
</span>
<div style="clear: both;"></div>
   </div>

<div class="xarbb-tablewrapper">



   <table>
       <tr>
           <th scope="col">
Author</th>
           <th scope="col">
Message</th>
       </tr>



            <tr>
                <td class="xar-norm author">
                    <div>
                        <a href="http://www.linux.com/archive/roles/405613">PerlCoder</a>
                   </div>
                   <div class="xar-sub">
Joined: Jun 30, 2008<br/>Posts: 148                        <br />
                        <a href="http://www.linux.com/archive/forums?by=405613&amp;func=searchtopics">
Other Topics</a>
                   </div>
               </td>

               <td class="xar-norm message">
                   <div class="xarbb-messagecontrols">
                       <div class="leftnote">
                           <span class="xar-sub">
Posted Dec 02, 2008 at 2:59:31 AM</span>
                       </div>



<div style="float: left;">
                           <strong class="xar-sub">
Subject: Creating binary file formats in C                            </strong>
                        </div>
                        <div style="clear: both;"></div>
                    </div>

                    <div>
                        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.<p class="hiddensignature">
PerlCoder (http://indicium.us)</p>
</div>

                </td>
            </tr>

            <tr>
                <td class="xar-norm author">
                    <a href="http://www.linux.com/archive/forums/topic/3561#top">
Back to top</a>
</td>
               <td class="xar-alt">

                        <a href="http://www.linux.com/archive/roles/405613">
                            <img src="modules/xarbb/xarimages/new/icon_profile.gif" alt="Profile" />
                        </a>
                        <a href="http://www.linux.com/archive/?module=roles&amp;func=email&amp;uid=405613">
                            <img src="modules/xarbb/xarimages/new/icon_email.gif" alt="Email" />
                       </a>
                            <a href="http://indicium.us">
                                <img src="modules/xarbb/xarimages/new/icon_www.gif" alt="Website" />
                           </a>
</td>
           </tr>





           <tr>
               <td class="xar-norm author">
                   <strong>

                            <a href="http://www.linux.com/archive/roles/401295">
                                tophandcwby                           </a>
</strong>

                   <span class="xar-sub">
<br />
Joined Apr 10, 2008<br/>Posts: 81<br />
                        <br />
                        <a href="http://www.linux.com/archive/forums?by=401295&amp;func=searchtopics">
Other Topics                        </a>
                    </span>
                </td>

                <td class="xar-accent message">
                   <div class="xarbb-messagecontrols">
                       <div class="leftnote">
                           <span class="xar-sub">
Posted:
                                Dec 06, 2008 12:59:11 PM                           </span>
                       </div>
                       <div class="rightnote">
</div>
                       <div style="float:left;">
                           <strong class="xar-sub">
Subject:
                                <a name="1204027" href="http://www.linux.com/archive/forums/topic/3561#1204027">
                                    Creating binary file formats in C                                </a>
                            </strong>
                        </div>
                        <div style="clear: both;"></div>
                    </div>

                    <div style="clear: left;">
                        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                    </div>
                </td>
            </tr>

            <tr>
                <td class="xar-norm author">
                    <a href="http://www.linux.com/archive/forums/topic/3561#top">
Back to top</a>
</td>

               <td class="xar-alt">

                            <a href="http://www.linux.com/archive/roles/401295">
                                <img src="modules/xarbb/xarimages/new/icon_profile.gif" alt="Profile" />
                            </a>
                            <a href="http://www.linux.com/archive/?module=roles&amp;func=email&amp;uid=401295">
                                <img src="modules/xarbb/xarimages/new/icon_email.gif" alt="Email" />
                           </a>
                                <a href="http://">
                                    <img src="modules/xarbb/xarimages/new/icon_www.gif" alt="Website" />
                               </a>
</td>
           </tr>

           <tr>
               <td class="xar-norm author">
                   <strong>

                            <a href="http://www.linux.com/archive/roles/416574">
                                nodan                           </a>
</strong>

                   <span class="xar-sub">
<br />
Joined Jan 09, 2009<br/>Posts: 1<br />
                        <br />
                        <a href="http://www.linux.com/archive/forums?by=416574&amp;func=searchtopics">
Other Topics                        </a>
                    </span>
                </td>

                <td class="xar-norm message">
                   <div class="xarbb-messagecontrols">
                       <div class="leftnote">
                           <span class="xar-sub">
Posted:
                                Jan 09, 2009 10:23:05 AM                           </span>
                       </div>
                       <div class="rightnote">
</div>
                       <div style="float:left;">
                           <strong class="xar-sub">
Subject:
                                <a name="1206081" href="http://www.linux.com/archive/forums/topic/3561#1206081">
                                    Creating binary file formats in C                                </a>
                            </strong>
                        </div>
                        <div style="clear: both;"></div>
                    </div>

                    <div style="clear: left;">
                        Don't.

Don't use binary (i.e. not readable in a text editor) file formats. Use xml instead.                    </div>
                </td>
            </tr>

            <tr>
                <td class="xar-norm author">
                    <a href="http://www.linux.com/archive/forums/topic/3561#top">
Back to top</a>
</td>

               <td class="xar-alt">

                            <a href="http://www.linux.com/archive/roles/416574">
                                <img src="modules/xarbb/xarimages/new/icon_profile.gif" alt="Profile" />
                            </a>
                            <a href="http://www.linux.com/archive/?module=roles&amp;func=email&amp;uid=416574">
                                <img src="modules/xarbb/xarimages/new/icon_email.gif" alt="Email" />
                           </a>
                                <a href="http://">
                                    <img src="modules/xarbb/xarimages/new/icon_www.gif" alt="Website" />
                               </a>
</td>
           </tr>

           <tr>
               <td class="xar-norm author">
                   <strong>

                            <a href="http://www.linux.com/archive/roles/386456">
                                Rubberman                           </a>
</strong>

                   <span class="xar-sub">
<br />
Joined Jul 30, 2007<br/>Posts: 944<br />
Location:40 miles west of Chicago<br />
                        <br />
                        <a href="http://www.linux.com/archive/forums?by=386456&amp;func=searchtopics">
Other Topics                        </a>
                    </span>
                </td>

                <td class="xar-accent message">
                   <div class="xarbb-messagecontrols">
                       <div class="leftnote">
                           <span class="xar-sub">
Posted:
                                Jan 10, 2009 3:46:44 AM                           </span>
                       </div>
                       <div class="rightnote">
</div>
                       <div style="float:left;">
                           <strong class="xar-sub">
Subject:
                                <a name="1206153" href="http://www.linux.com/archive/forums/topic/3561#1206153">
                                    Re:  Creating binary file formats in C                                </a>
                            </strong>
                        </div>
                        <div style="clear: both;"></div>
                    </div>

                    <div style="clear: left;">
                        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 &quot;cheap&quot; 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!<p class="hiddensignature">
Sometimes real fast is almost as good as real time.
Remember, Google is your friend!</p>
                    </div>
                </td>
            </tr>

            <tr>
                <td class="xar-norm author">
                    <a href="http://www.linux.com/archive/forums/topic/3561#top">
Back to top</a>
</td>

               <td class="xar-alt">

                            <a href="http://www.linux.com/archive/roles/386456">
                                <img src="modules/xarbb/xarimages/new/icon_profile.gif" alt="Profile" />
                            </a>
                            <a href="http://www.linux.com/archive/?module=roles&amp;func=email&amp;uid=386456">
                                <img src="modules/xarbb/xarimages/new/icon_email.gif" alt="Email" />
                           </a>
                                <a href="http://">
                                    <img src="modules/xarbb/xarimages/new/icon_www.gif" alt="Website" />
                               </a>
                                <a href="aim:goim?screenname=vikrant48&amp;message=Hello+Are+you+there?">
                                    <img src="modules/xarbb/xarimages/new/icon_aim.gif" alt="AOL Instant Messenger" />
                               </a>
</td>
           </tr>
</table>
</div>

<div class="xarbb-topiccontrols">

<span class="xar-sub">
    <a href="http://www.linux.com/archive/forums">
Forum Index    </a>
    <span style="font-size: larger;">&#187;</span>
    <a href="http://www.linux.com/archive/forums/category/4181">
        Forums 
    </a>
    <span style="font-size: larger;">&#187;</span>
    <a href="http://www.linux.com/archive/forums/forum/5">
        Programming and Development   </a>
</span>

</div>



            </channel>
</rss>
