Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Storing data structure

Reply
Thread Tools

Storing data structure

 
 
TDB
Guest
Posts: n/a
 
      01-27-2008
Hello,

I'm creating an application using C which requires the data structures
like trees and graphs to be stored in files and retrieved later
( simply serialization of a data structure ) .

Is there any libraries available in C for this purpose ?
 
Reply With Quote
 
 
 
 
santosh
Guest
Posts: n/a
 
      01-27-2008
TDB wrote:

> Hello,
>
> I'm creating an application using C which requires the data structures
> like trees and graphs to be stored in files and retrieved later
> ( simply serialization of a data structure ) .
>
> Is there any libraries available in C for this purpose ?


The Standard library functions fread/fwrite should be sufficient
primitives for this purpose.

 
Reply With Quote
 
 
 
 
jacob navia
Guest
Posts: n/a
 
      01-27-2008
TDB wrote:
> Hello,
>
> I'm creating an application using C which requires the data structures
> like trees and graphs to be stored in files and retrieved later
> ( simply serialization of a data structure ) .
>
> Is there any libraries available in C for this purpose ?


You will have to do that yourself, i.e. you will have to transform
the embedded pointers into pointers to other file structures.

In a very general form, you should be able to translate pointers into
indexes into a node array. When you read back the node array you
transform again those indexes into real pointers.



--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      01-27-2008
santosh said:

> TDB wrote:
>
>> Hello,
>>
>> I'm creating an application using C which requires the data structures
>> like trees and graphs to be stored in files and retrieved later
>> ( simply serialization of a data structure ) .
>>
>> Is there any libraries available in C for this purpose ?

>
> The Standard library functions fread/fwrite should be sufficient
> primitives for this purpose.


You're right, but I don't think he was asking for primitives. He was asking
for a library which "understands" the serialisation of trees and graphs in
a way that primitives don't. You can't just fwrite(root, sizeof *root, 1,
fp) and expect the whole tree to be saved.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      01-27-2008
santosh <> writes:
> TDB wrote:
>> I'm creating an application using C which requires the data structures
>> like trees and graphs to be stored in files and retrieved later
>> ( simply serialization of a data structure ) .
>>
>> Is there any libraries available in C for this purpose ?

>
> The Standard library functions fread/fwrite should be sufficient
> primitives for this purpose.


Certainly, but I think that misses the OP's point. You have to
flatten the data structure somehow, replacing any pointers with
something more "static" (not in the C keyword sense), before you can
fwrite anything.

I don't know of any libraries to do this, but they probably exist.

--
Keith Thompson (The_Other_Keith) <kst->
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Willem
Guest
Posts: n/a
 
      01-27-2008
TDB wrote:
) Hello,
)
) I'm creating an application using C which requires the data structures
) like trees and graphs to be stored in files and retrieved later
) ( simply serialization of a data structure ) .
)
) Is there any libraries available in C for this purpose ?

A library can only be expected to save a data structure in a file
if it knows all the details of that data structure, and this is usually
only the case if that library also provides those structures.
So, IMO your best bet would be to look for tree- and graph-libraries.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
Reply With Quote
 
Malcolm McLean
Guest
Posts: n/a
 
      01-27-2008

"TDB" <> wrote in message news
> I'm creating an application using C which requires the data structures
> like trees and graphs to be stored in files and retrieved later
> ( simply serialization of a data structure ) .
>
> Is there any libraries available in C for this purpose ?
>

Yes and no.
One cannot store pointers in a file. Whilst there are general-purpose
libraries that purport to be able to serialise structures, using a
programmer-specified format string, these are so difficult to use that I
think it is red herring to recommend them.
However storing a tree is not too difficult. Simply write a bracket,
recursively write the subtree, and then write a closing bracket.

Loading is more tricky.
If you look for the Newick tree loader on my website you might find what you
are looking for. However it will only load trees, not graphs.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      01-27-2008
TDB wrote:

> Hello,
>
> I'm creating an application using C which requires the data structures
> like trees and graphs to be stored in files and retrieved later
> ( simply serialization of a data structure ) .
>
> Is there any libraries available in C for this purpose ?


One link I found through Google is:

<http://tpl.sourceforge.net/>

HTH

 
Reply With Quote
 
Friedrich Dominicus
Guest
Posts: n/a
 
      01-27-2008
TDB <> writes:

> Hello,
>
> I'm creating an application using C which requires the data structures
> like trees and graphs to be stored in files and retrieved later
> ( simply serialization of a data structure ) .
>
> Is there any libraries available in C for this purpose ?

As others have pointed out without some work on your side this won't
work. Howerver you may check the berkeley DB stuff which offers quite
a bunch for "persitence in C"

http://www.oracle.com/database/berke.../db/index.html

Regards
Friedrich


--
Please remove just-for-news- to reply via e-mail.
 
Reply With Quote
 
cr88192
Guest
Posts: n/a
 
      01-27-2008

"TDB" <> wrote in message
news:60f15d9d-dd7c-4975-aac6-...
> Hello,
>
> I'm creating an application using C which requires the data structures
> like trees and graphs to be stored in files and retrieved later
> ( simply serialization of a data structure ) .
>
> Is there any libraries available in C for this purpose ?



well, there are a lot of libs for a lot of things, so a lot depends on what
you want to do.


if XML is acceptable, than maybe either SAX or DOM may be what you are
looking for.
libxml is a common choice here.

many projects may implement other representations as well, for example,
Lisp-style S-Expressions may well be a good starting point for a customized
(yet still "general") data processing/serialization system (this depends a
lot on what is being done, but anymore, I will generally lean more in favor
of XML than S-Expressions).


also common is implementing a format based on line-oriented text files.

in this way, we can use fprintf, fgets, and sscanf (actually, I more usually
implement a 'split' function here), in order to serialize and reload the
data.

this approach is often both simple and allows fast loading/saving, but is
less general than SAX or DOM (essentially, the structure of the data and the
structure of the file are tied together).

as such, this is good for dumping and reloading a bunch of context-specific
data (such as tables, 3D geometry, ...) but is not so good for more
free-form data (such as compiler AST trees, hypertext documents, ...).

for XML, this is fairly close to the SAX approach.


another common approach, is to regard the file as a serialized bytestream or
similar.

in these cases, we often have loaders that, for example, use fgetc to read
individual bytes (or sometimes, FOURCC's and similar), and dispatch to the
appropriate handlers as needed (read the magic value for a node, so call the
function to handle reading nodes, ...).

this approach tends to lead to formats that are both fairly dense, and have
a good tradeoff between speed and flexibility (textual formats tend to be
much bigger and slower, and data-dumping formats tend to be brittle).

however, as a cost, these formats are not human-readable (unlike most
textual formats), and are typically much slower than raw dumping.

I suspect, however, for widely used binary formats, variations of this
approach are the most common.


another common approach is data dumping:

freading/fwriting data is possible, but as noted, may require a lot of
processing to serialize and reload the data. for this approach, it is common
practice to avoid any use of pointers within these structures (instead,
indices and offsets are used almost exclusively, and the app will operate on
the data more or less as it appears in the files).

for example, I have seen loaders that will fread a whole file structured
like this, and then create a context with:
a pointer to the file's data;
a set of specific pointers to specific parts of the files' data (such as the
strings tables, nodes table, ...).

other times, specific tables are read, and placed within different buffers.


when handling accesses, we will use indices or offsets, to access the
specific members.
j=ctx->node[i].left;
k=ctx->node[i].right;
s=ctx->strtab+ctx->node[i].name;


other times, these files may be split up into some number of chunks or
blocks, which are loaded, processed, and/or stored, as needed. this
variation is common within things like database engines (for example, for
implementing structures like B-Trees).

the advantage of this approach is that file loading and saving can be made
to operate very quickly (for a save, we simply dump the whole file's
contents back to disk, or, in some cases, unmap the file from memory).

the disadvantage, is that these kind of file formats can become excessively
brittle (these are the kinds of files where you often find version numbers,
endianess and alignment check values, ..., embedded in the headers).

as a result, these formats are usually used within a specific version of a
specific app, and very rarely become common-use. very often, these formats
are also not formally specified either (when the code or data changes, so
does the file format...).


dunno if this helps any...


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple structure and copying data to pointer of the same structure A C++ 27 04-16-2011 11:07 PM
DNA String Compression For Storing in Data Structure Gundala Viswanath C++ 1 01-17-2009 07:01 PM
User Images: Storing in Files VS Storing in Database Jonathan Wood ASP .Net 1 06-02-2008 05:56 PM
storing pointer vs storing object toton C++ 11 10-13-2006 11:08 AM
Graph Data structure to storing components in spice netlist.. Jonny C++ 2 05-02-2006 08:59 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57