I am trying to find how to store and process (search, add, remove) an array of linked lists on disk. For example in memory, it would like
struct list {
int a;
struct list *next;
}LIST
LIST *array[ARRAY_SIZE]
int main{
...
LIST *foo = array[pointer];
/* Search */
while(foo!=NULL){
...
foo=foo->next
}
}
- I believe that by using fseek() I can point to a specific element/structure of the array in the file. But I cannot understand if all previous elements need to have been written or not.
- Can this be done with dynamic allocation on disk?
- How will I link on element to another one in the linked list?
Any example would certainly help!
Okay, as Amardeep says, this sounds like the sort of thing that would best be done practically using some kind of database, like, or, Berkeley DB. But let’s answer the questions anyway.
you can indeed use fseek (or its system call, lseek) to determine where something is on the disk and find it later. If you use some method of computing the offset, you’re implementing what we used to call a direct file; otherwise you might store an index, which leads you off toward the indexed sequential method.
whether it can be done with dynamic allocation sort of depends on the file system. Many UNIX file systems support * sparse* allocation, which means that if you allocate block 365 it doesn’t have to allocate blocks 0 through 364. Some dont.
Let’s say that you have a structure with a length k that looks more or less like this:
(trick the parse)
You create the first item; set its block number to 0. Set next to some distinguished value, say -1.
You now have two items on disk. keep this up and you’ll eventually have a thousand items on disk. Now, to find item # 513, you just
You need a buffer; since we freed the previous ones we’ll make another
Read that many bytes
And poof record 513 is in memory pointed to by
b. Get the record following with