Operating System - HP-UX
1753479 Members
5229 Online
108794 Solutions
New Discussion юеВ

Re: Store C struct in binary file

 
SOLVED
Go to solution
Alex Lavrov.
Honored Contributor

Store C struct in binary file

Hello,

I'm looking for a standart way to store a C structure in a binary file. I never used this kind of thing and after some googling I still did not find the answer.

Actually, I'm looking for some really low-level storage mechanism in binary files. I know there some nice programs like berkeley db, but it's not good enough because they are GPL or commercial. I'm not sure that the program I'm writing gonna be GPL and also I don't plan to pay any money.

Thanx.
I don't give a damn for a man that can only spell a word one way. (M. Twain)
6 REPLIES 6
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Store C struct in binary file

Well, if you want low-level i/o that means file descriptors, read()'s, write()'s lseek()'s, open()'s and close()'es. The attached C code pasted from some of my standard routines should work and give you a very good idea of what is involved.



If it ain't broke, I can fix that.
Gregory Fruth
Esteemed Contributor

Re: Store C struct in binary file

If you want to stick to ANSI stdio (instead
of the POSIX read(), write(), etc.) you can
also use fread() and fwrite(). If you want
to be platform-independent you should
probably check out XDR too (man xdr).

Alex Lavrov.
Honored Contributor

Re: Store C struct in binary file

Thanx for the replies, I still had no time to check it, but I will (and also assign points of course).


Alex.
I don't give a damn for a man that can only spell a word one way. (M. Twain)
Alex Lavrov.
Honored Contributor

Re: Store C struct in binary file

Thank you, it's exactly what I'm looking for.


Alex.
I don't give a damn for a man that can only spell a word one way. (M. Twain)
TwoProc
Honored Contributor

Re: Store C struct in binary file

Alex, it's surprisingly simpler than you think - the source of the fwrite buffer would be the pointer to the memory structure you created. Ditto for the fread function - just pass a pointer to the memory structure that you created it will load back what you wrote out from the previous fwrite.

Let's say your memory structure "mystruct" is already defined - and your file "MYFILE" is already opened .

fwrite (&mystruct, (size_t)sizeof(mystruct),1,MYFILE);

to get it back:
fread (&mystruct, (size_t)sizeof(mystruct),1,MYFILE);

If you've created an array of these structures - you can save them all by setting your pointer to the first one - and passing the number of elements in your "array" as the third argument - then they'll all be passed.

Be aware that due to padding in some compilers, you could do this one compiler, and the padding could be off if you read back the same data file in another. Doesn't always happen, it's just that you should check b/w compilers if that is necessary.
We are the people our parents warned us about --Jimmy Buffett
Alex Lavrov.
Honored Contributor

Re: Store C struct in binary file

Thanx.
I don't give a damn for a man that can only spell a word one way. (M. Twain)