|
-
May 14th, 2010, 09:43 AM
#1
learning binary files.
currently I only know something like:
fstream fstream_Object.open(/*opening info here*/);
and looping
"fstream_Object<<Atomic_type_to_file;" or "fstream_Object>>Atomic_type_from_file;"
to get information saved to and from a file. I'm trying to learn how to use binary input and output. I looked up .bmp specifications and learned that binary files need to have a "fileheader" a "infoHeader"and a "data block" that all work together somehow.
So the question is how dose puting the three together work?
Is the file header similar to creating a "struct" and then the info header tells you where in the data block to fill each saved struct and how many?
A simpler example than a .bmp file might be something like:
struct Elements
{
unsigned int number;
unsigned int weight;
};
Elements element[118];
and place this into a file with it's own header and such.
-
May 14th, 2010, 09:48 AM
#2
Re: learning binary files.
If you're designing a binary format, you can arrange it however you like. It's just a series of bits, which you happen to know how to turn into meaningful information.
If you're trying to work with someone else's binary format, then find and read the documentation. It should tell you what you need to know.
-
May 14th, 2010, 10:04 AM
#3
Re: learning binary files.
 Originally Posted by Lindley
If you're trying to work with someone else's binary format, then find and read the documentation. It should tell you what you need to know.
It's the documentation that has me confused. I'm trying to learn how the system of using headers, data blocks and such works together. I'm told you shouldn't just dump information strait from memory into a file which gets confusing.
For my example I would have to create everything from scratch. As for the .bmp, I have yet to understand going from the 320x480 RGB pixels my paint program shows me to the complicated way it's represented in the file.
-
May 14th, 2010, 10:08 AM
#4
Re: learning binary files.
 Originally Posted by ehoi
It's the documentation that has me confused. I'm trying to learn how the system of using headers, data blocks and such works together. I'm told you shouldn't just dump information strait from memory into a file which gets confusing.
For my example I would have to create everything from scratch. As for the .bmp, I have yet to understand going from the 320x480 RGB pixels my paint program shows me to the complicated way it's represented in the file.
A binary file can have any format you want it to. There's really no concept of headers and data blocks you need to worry about. Are you asking about bitmaps or binary files in general?
-
May 14th, 2010, 10:28 AM
#5
Re: learning binary files.
I was talking about binary files in general. I thought using headers such was a common convention ( " http://www.magicdb.org/filedesign.html " for an example).
If I wanted to use a header style of saving information i would need to understand how they work. So, in that respect, I guess i am asking about .bmp files too. (to understand how to go about doing it, or even make sense of someone elses files.)
-
May 14th, 2010, 10:37 AM
#6
Re: learning binary files.
You can worry about designing a "good" format later. To start with, just figure out what you actually need and go from there.
-
May 14th, 2010, 10:42 AM
#7
Re: learning binary files.
 Originally Posted by ehoi
I was talking about binary files in general. I thought using headers such was a common convention ( " http://www.magicdb.org/filedesign.html " for an example).
If I wanted to use a header style of saving information i would need to understand how they work. So, in that respect, I guess i am asking about .bmp files too. (to understand how to go about doing it, or even make sense of someone elses files.)
It really depends what you want to do. For a complex and evolving format, there's some solid advice in there, but it's not always required and not always appropriate.
-
May 17th, 2010, 12:37 AM
#8
Re: learning binary files.
Here's an example of where using a header would be useful.
Code:
struct FileHeader
{
unsigned long FileID; // The type of file so you can recognize if it's valid data
unsigned short HighVersion; // The high version
unsigned short LowVersion; // Low Version
unsigned long OffsetToData; // Offset in file to data entries, this allows
// other "tag" headers to follow file header
// such as author tags/etc...
unsigned long DataEntryCount; // The number of data entries in the file
};
struct ContactInfo // This is the type of "data entry"
{
char FirstName[16];
char LastName[24];
unsigned short BirthMonth; // 1 = January, 12 = December
unsigned char BirthDay;
unsigned short BirthYear;
char AddressLine1[64];
char AddressLine2[32];
char City[32];
char State[24];
char ZipCode[5];
char PhoneNumber[10];
char EmailAddress[40];
};
// set the data members in the header, write the header (if any other tags, write them), write the data entries at the correct offset
// read the data members into a header variable, (if any other tags, read them), seek to the data enty offset, read data entries
 Originally Posted by ehoi
I was talking about binary files in general. I thought using headers such was a common convention ( " http://www.magicdb.org/filedesign.html " for an example).
If I wanted to use a header style of saving information i would need to understand how they work. So, in that respect, I guess i am asking about .bmp files too. (to understand how to go about doing it, or even make sense of someone elses files.)
Yes, it IS a common convention to use headers for binary files. Almost every binary data file needs a description of what it contains to SOME EXTENT. Like a count of data entries, etc... There are exceptions, few, but there are. One example would be that every input file for a given program is atleast supposed to be GUARANTEED to contain data that is always in the same format, ie... pixel data that is ALWAYS at the same resolution and color depth. BITMAPS on the other hand, are NOT GUARANTEED to be the same resolution/color depth/etc... so header data is NECESSARY to describe what type of data resides in the file.
Last edited by CppCoder2010; May 17th, 2010 at 12:43 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|