Unix Programming - structure of data to file

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > September 2005 > structure of data to file





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author structure of data to file
hirenshah.05@gmail.com

2005-09-25, 5:53 pm

how can i write structures of data in file?
do i have have to write individual parameter of structure or is there
anyway where i can pass whole structure?

Roger Leigh

2005-09-25, 5:53 pm

hirenshah.05@gmail.com writes:

> how can i write structures of data in file? do i have have to write
> individual parameter of structure or is there anyway where i can
> pass whole structure?


This depends on the kind of structure you have. If it's
self-contained (i.e. does not contain pointers), you could write out
the struct directly using write() or fwrite(). Note that this is
non-portable, irrespective of whether it works or not.

Another, more portable approach, is to write a function to write the
object state out as text, and another one to read in the text and
reconstruct the object. If you have deeply-nested structures, XML is
one way you can do this, though not everyone would agree that it's
necessarily the best way

The language used may also support this natively, e.g. Java. Using C
and GObject introspection, it's possible to construct objects quite
simply from a text stream. See
http://cvs.alioth.debian.org/cgi-bi...ot=buildd-tools
for an example (sbuild_chroot_new_from_keyfile and
sbuild_chroot_set_properties_from_keyfil
e handle deserialisation,
while sbuild_chroot_print_config and the print_config virtual function
handle serialisation, including for derived classes).
http://cvs.alioth.debian.org/cgi-bi...ot=buildd-tools
gives an example of the text format.

Have a Google for "serialisation" and "deserialisation".


Regards,
Roger

--
Roger Leigh
Printing on GNU/Linux? http://gimp-print.sourceforge.net/
Debian GNU/Linux http://www.debian.org/
GPG Public Key: 0x25BFB848. Please sign and encrypt your mail.
SM Ryan

2005-09-25, 5:53 pm

Roger Leigh <${rleigh}@invalid.whinlatter.ukfsn.org.invalid> wrote:
# hirenshah.05@gmail.com writes:
#
# > how can i write structures of data in file? do i have have to write
# > individual parameter of structure or is there anyway where i can
# > pass whole structure?
#
# This depends on the kind of structure you have. If it's
# self-contained (i.e. does not contain pointers), you could write out
# the struct directly using write() or fwrite(). Note that this is
# non-portable, irrespective of whether it works or not.

In C (this is Unix, so it's C or nothing), the compiler is allowed
to insert additional padding bytes into a struct that don't belong
to any field. The different compilers can insert different padding.
Different releases of a compiler can insert different padding. Even
compiling with the same compiler with different options can change
the padding. Phase of the moon can effect padding.

So when you naively write a struct out en masse to a file, you have
no guarentee you can read the bytes back into the same struct.

Pointers can be written out to a file as a sequence of bytes. They're
just not likely to be meaningful when reread,

# Another, more portable approach, is to write a function to write the
# object state out as text, and another one to read in the text and

Different computers can have different integers, bytes orders, reals,
etc. Text is a more portable format than binary. Even character set
changes can be applied to text transparently.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
There are subtler ways of badgering a witness.
Måns Rullgård

2005-09-25, 5:53 pm

SM Ryan <wyrmwif@tango-sierra-oscar-foxtrot-tango.fake.org> writes:

> Roger Leigh <${rleigh}@invalid.whinlatter.ukfsn.org.invalid> wrote:
> # hirenshah.05@gmail.com writes:
> #
> # > how can i write structures of data in file? do i have have to write
> # > individual parameter of structure or is there anyway where i can
> # > pass whole structure?
> #
> # This depends on the kind of structure you have. If it's
> # self-contained (i.e. does not contain pointers), you could write out
> # the struct directly using write() or fwrite(). Note that this is
> # non-portable, irrespective of whether it works or not.
>
> In C (this is Unix, so it's C or nothing), the compiler is allowed
> to insert additional padding bytes into a struct that don't belong
> to any field. The different compilers can insert different padding.
> Different releases of a compiler can insert different padding. Even
> compiling with the same compiler with different options can change
> the padding. Phase of the moon can effect padding.
>
> So when you naively write a struct out en masse to a file, you have
> no guarentee you can read the bytes back into the same struct.


If the file is to be read back later during the same run of the
program, it is safe to just dump the struct contents. In other cases,
all bets are off.

> Pointers can be written out to a file as a sequence of bytes. They're
> just not likely to be meaningful when reread,


I wouldn't do that even when reading during the same session.

> # Another, more portable approach, is to write a function to write the
> # object state out as text, and another one to read in the text and
>
> Different computers can have different integers, bytes orders, reals,
> etc. Text is a more portable format than binary. Even character set
> changes can be applied to text transparently.


Indeed. Text formats are also much easier to debug.

--
Måns Rullgård
mru@inprovide.com
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com