Sign in to follow this  
Komal Shashank

What exactly is a file header?

Recommended Posts

Hi... I'm new to GameDev.net. This is my first post. Can someone please tell me: What is a file header? Why is it used? And what would I need to do to write a file header to a file or read a file header from a file in binary using fstream?

 
Searching for this on Google turns up results for header files (.h). Nothing for file header. Please help. Thank you.

Share this post


Link to post
Share on other sites

OK... So if I wanted to write in C++, say a vector of integers (std::vector<int>), in binary format, I just ofstream their binary representation as bytes into the file? Also, how would I separate the file's data from it's header while writing so that when reading it will be distinguished respectively and treated as such (header as header and data as data)?

Edited by WDRKKS

Share this post


Link to post
Share on other sites

A file header is part of the file, it's not really separate from it. It's just a number of bytes that's defined (in the file's format description) as having some specific meaning.

 

So you don't do anything special to read or write it, it's just some more data.

 

For your own file format, you could define a struct that holds a value to identify the file as something that you know (some pre-defined 32bit integer value, for example) and then some useful information (count of the items you've written, for example).

Share this post


Link to post
Share on other sites

Thank you, frob! That is a very detailed and informative explanation. I do have one question though... All the different types of data that you mention that can be stored in the header, can all this be declared inside a struct and written normally at the start of the file just like the payload data? Please clarify. Thanks.

Edited by WDRKKS

Share this post


Link to post
Share on other sites

Thank you, frob! That is a very detailed and informative explanation. I do have one question though... All the different types of data that you mention that can be stored in the header, can all this be declared inside a struct and written normally at the start of the file just like the payload data? Please clarify. Thanks.

 

That's pretty much how that would work, yeah. Fill out your header struct in your code and just write it to the beginning of your file. At read time you just read in this struct again from the beginning of the file and you're good to go

Share this post


Link to post
Share on other sites

Just be sure to notice one of the extremely important details often overlooked by beginners:

 


001Eh                   1 byte   Version number

 

File versions, or knowing what version the format uses, is vitally important.

 

Very frequently during development and during updates you may add a value, remove a value, or change a value's meaning. You absolutely need a version number in the file so you can update the data to whatever the latest and greatest needs to be, or to block a file from a future edition from loading if you won't recognize the contents.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this