| View previous topic :: View next topic |
| Author |
Message |
David Instone Guest
|
Posted: Tue Dec 05, 2006 7:18 pm Post subject: Representing binary data |
|
|
I have a requirement to write some data to a file in a predefined format and
have therefore set up a structure for that. The data is however mixed char
and integer form and from my understanding there is no cross platform
guarantee of byte length for integer variables. So I am using char arrays
for all.
struct Header
{
unsigned char ChunkID[4];
unsigned char ChunkSize[4];
unsigned char BlockAlign[2];
etc...
}
While ChunkID is no problem as a character array both ChunkSize and
BlockAlign are integer values that need to be written to the file as 4 and
two bytes respectively. They also have to be written as little endian form
to make matters worse. Is there a better way to do this?
Next I have to write it all to a file and I dont want to scare anyone but I
still like fopen() as I dont understand those Cout>> thingy's yet. Does
anybody know of a good tutorial that involves writing a structure to disk
using that stuff? Or should I just write it out one byte at a time as
planned.
Many thanks |
|
| Back to top |
|
 |
Hans Galema Guest
|
Posted: Tue Dec 05, 2006 8:07 pm Post subject: Re: Representing binary data |
|
|
David Instone wrote:
| Quote: | Next I have to write it all to a file and I dont want to scare anyone but I
still like fopen() as I dont understand those Cout>> thingy's yet. Does
anybody know of a good tutorial that involves writing a structure to disk
using that stuff?
|
You do not need a tutorial for that. Just consider the struct as a
buch of bytes and write them with one call of fwrite().
fwrite( &Header, sizeof ( Header ) );
//well like that. I did not check the actual parameters
Hans. |
|
| Back to top |
|
 |
Bob Gonder Guest
|
Posted: Tue Dec 05, 2006 9:17 pm Post subject: Re: Representing binary data |
|
|
David Instone wrote:
| Quote: | I have a requirement to write some data to a file in a predefined format and
have therefore set up a structure for that
|
Good.
| Quote: | The data is however mixed char
and integer form and from my understanding there is no cross platform
guarantee of byte length for integer variables. So I am using char arrays
for all.
|
Ok, but you could use WORD and DWORD macros too, which translate into
short and long.
| Quote: | They also have to be written as little endian form
to make matters worse.
|
Borland/MS/Intel are little endian by hardware design.
But if you need your application to recompile on a big endian
compiler, then you might have pre/post-read/write endian swappers that
do nothing on Borland/Intel compilers.
The easiest way would be to have the application Not use fread, but
perhaps MyFread which would do the swap after the fread.
Also MyFwrite would SwapStruct();fwrite();SwapStruct();
| Quote: | Next I have to write it all to a file and I dont want to scare anyone but I
still like fopen() as I dont understand those Cout>> thingy's yet. Does
anybody know of a good tutorial that involves writing a structure to disk
using that stuff?
|
First make sure you always fopen in binary mode.
filehandle = fopen( "filename", "rb+" );
Position to the proper offset in the file
fseek( filehandle, RecordNumber * RecordSize, SEEK_SET );
The file may not be greater than 2 GB with this method.
Update the file.
fwrite( &Struct, sizeof(Struct), 1, filehandle );
If you are just streaming output to a new file, you don't need the
fseek.
| Quote: | Or should I just write it out one byte at a time as
planned.
|
Eeeek!
Please never do that again. |
|
| Back to top |
|
 |
Ed Mulroy Guest
|
Posted: Tue Dec 05, 2006 10:14 pm Post subject: Re: Representing binary data |
|
|
In addition to the good advice others have given you, there also is the
question of alignment.
For efficiency elements in structures are arranged to align on memory
addresses which are optimized for the hardware. This is ok for data that
always will be running on Intel x86 processors but you wish to have it work
cross-platform to other processors. To do that I think you should pack the
structure on byte boundaries.
You can do the packing in this way:
#pragma pack(push, 1) // save current settings, set byte alignment
struct Header
{
unsigned char ChunkID[4];
unsigned char ChunkSize[4];
unsigned char BlockAlign[2];
etc...
}
#pragma pack() // restore the settings
.. Ed
| Quote: | David Instone wrote in message
news:45757170$1 (AT) newsgroups (DOT) borland.com...
I have a requirement to write some data to a file in a predefined
format and have therefore set up a structure for that. The data is
however mixed char and integer form and from my understanding
there is no cross platform guarantee of byte length for integer
variables. So I am using char arrays for all.
struct Header
{
unsigned char ChunkID[4];
unsigned char ChunkSize[4];
unsigned char BlockAlign[2];
etc...
}
While ChunkID is no problem as a character array both ChunkSize
and BlockAlign are integer values that need to be written to the file
as 4 and two bytes respectively. They also have to be written as little
endian form to make matters worse. Is there a better way to do this?
Next I have to write it all to a file and I dont want to scare anyone but
I
still like fopen() as I dont understand those Cout>> thingy's yet. Does
anybody know of a good tutorial that involves writing a structure to disk
using that stuff? Or should I just write it out one byte at a time as
planned. |
|
|
| Back to top |
|
 |
Thorsten Kettner Guest
|
Posted: Wed Dec 06, 2006 2:38 pm Post subject: Re: Representing binary data |
|
|
Bob Gonder <notbg (AT) notmindspring (DOT) invalid> wrote:
| Quote: | David Instone wrote:
[snip]
Or should I just write it out one byte at a time as planned.
Eeeek!
Please never do that again.
|
Why not? If his app is on a big endian machine and he wants to
write little endian, he will write byte by byte. Or is there
something I have misunderstood? |
|
| Back to top |
|
 |
David Instone Guest
|
Posted: Thu Dec 07, 2006 9:10 am Post subject: Re: Representing binary data |
|
|
Thank you very much everybody, it works like a charm now. The WORD and DWORD
macros simplified things dramaticly and I used the fwrite solution to dump
it to disk. The hard part came later when I had to figure out which expert
on the net was giving me the correct form of the data for the structure
Ah well, trial and error makes programming fun doesn't it.
Thanks again |
|
| Back to top |
|
 |
Bob Gonder Guest
|
Posted: Sun Dec 10, 2006 1:40 am Post subject: Re: Representing binary data |
|
|
Thorsten Kettner wrote:
| Quote: | Bob Gonder wrote:
David Instone wrote:
[snip]
Or should I just write it out one byte at a time as planned.
Please never do that again.
Why not? If his app is on a big endian machine and he wants to
write little endian, he will write byte by byte. Or is there
something I have misunderstood?
|
If he writes it out one byte at a time, it will be very slow.
If he swaps the bytes in the structure, then one write of the struct
will be fast. |
|
| Back to top |
|
 |
|