Click to See Complete Forum and Search --> : C Bin tree help writing/reading to/from file
acid45
04-17-2003, 06:40 PM
Well I have made an atempt at it and I'm not very good with recursive code writing so...I'm kinda screwed I'm trying to read a file and put all of the elements from the file into a bin search tree and then being able to delete add and edit each of these nodes after they've been put into bin search tree. I kinda have an idea how to write them back to the file but no idea how to read it out of the file...here is what I have so far for reading from the file:
void main()
{
...
FILE *file;
file = fopen("library.dat","r");
if(!file)
{
printf("Error Opening File\n");
}
else
{
while(!feof(file))
{
//use fseek or soemthign like that to figure out the
//ammount of bytemsand then divide by the
//sizeof(struct node) and then move to the middle one and
//THEN do fread...liek display has to be recursive
fread(work,sizeof(struct node),1,file);
}
fclose(file);
...
}
However I don't have the right or left pointers in the struct for the nodes moving becaiuse I have no idea how ot do this. For writing to a file this is what I have:
void main()
....
file = fopen("library.dat","w");
if(!file)
{
printf("Error Opening File\n");
}
else
{
work = root;
writelist(work);
}
fclose(file);
//bunch of free statments for pointers used this is at the end of
//the main functions
}
//the writelist function is below
void writelist(struct node *wwork)//works
{
if(wwork != NULL)
{
writelist(wwork->left);
fwrite(wwork,sizeof(struct filenode),1,file);
writelist(wwork->right);
}
}
I can't test this until I get the read section done and I'm completelt screwed I can't think of anything other than what my comments say... am I on the right track???
bwkaz
04-17-2003, 08:09 PM
Code tags use square brackets. ;) No big deal, but for the future...
As to the actual problem, it might help to think in terms of what your writelist function is doing. Alternatively, it might be worthwhile to read each node in, all into a huge array. Then, create your tree from the array, starting in the middle (like you were saying).
However, note that the pointer values that you write out in your writelist function will not work -- you'll have to either point into the array, or do something with copying values out into other pieces of memory. If you use an array, make sure you malloc() it rather than creating it on the stack.
acid45
04-17-2003, 08:47 PM
I purposly used the <> instead of [] because I knew the UBB codes or whatever codes these are used [] and I was just marking out the code using HTML brackets :P. Ha I just looked to see what the [] with code do, good idea... :D..anyway... I'll take that into mind, thanks. malloc the array? Never did that before, neve thought I could. I only used malloc with pointers. I've only been into C since January. From what you said in your second paragraph I think you're saying that I can't do the same thing as my writelist function to read. Am I right? Thanks for the help. I should be able to use fseek to find how man elements of the array I need right but I don't know how to dynamically create an array, would a linked list work for this or should I just make an array bigger than I would think would be entered into the bin tree? Oh memory is kind of na issue on the machine that this program is going to be running on...only 32 MB or RAM so an array bigger than 16,000,000 would screw the system and eat half it's memory and I don't know how the program is going to be used and it might be edited to auto fill a bin tree with millions of nodes, but this alone would eat the memory so I think I'm just over tihnking the problem, I don't know... Anyway thanks I'll try that.
On another note...I founda song it's prety funny... Here are the lyrics:
We don't have a weapons plant.
In Iraq we just have sand.
I've been good since '92.
And now it looks like I am screwed.
I should pack, pack for a trip.
'Cause my radar is showing a bunch of blips.
And I started to realize.
that I Sadam will be pulvarized.
Bush F***en' hates me. Alalalalalala
George Bush, he ****en hates me. Alalalalala
Oh yes he does.
They tried so hard to find my weapons but we got none.
So please let me stay.
We will fight , they'll feel our power.
Well we might last, oh, about an hour.
I kill my own here in Bagdhad. oioioioioioi
No sign of guilt, I'm stock raving mad! oioioioioioi
They'll attack with their planes and their sihps. Aalalalala
Ans plus a bunch of men from the birtish. oioioioioioi
George Bush staryted to stratagize.
He decided to spread those lies.
Bush F***en' hates me. Alalalalalal
Yes he does.
Bush F***en' hates me. Alalalallala.
Oh yes he does.
They tried so hard but we told the UN that we got none. oioioioioioi
So please go away. Alalalalala
oioioioioioi alalalalalal oioioioioioi alalalalalal oioioioioioi
Please keep in mind that it's a song, and don't take offence to art...it's art afterall.
If there is a big anti-sadam song movement then I will remove it, I like it. It would be interesting to see a video of it, it's kind of a rock style song, guitar and drums but there are also middle east instruments too like...umm drums :P What middle-east(ians) can't have drums? :D
bwkaz
04-18-2003, 10:18 AM
Well, you know what I'd do? Write it in Java, and use Java's inherent ability to write out all objects that any single object references. Then, read the whole thing back in at once, with the same call. ;)
Umm, yeah, you probably don't want to malloc enough memory to hold all of them if you're not sure how it'll be used in the future.
Is there a way you could save the file in preimage order? That is, at each node, fwrite the node, then go left, then go right? That would make reading it in much easier -- all you'd have to do is read in the first thing, check whether the read-in left pointer is NULL or not, and if not, create another node (pointed to by the in-memory left pointer -- overwrite what you read in) and recurse on that. Then, if the right pointer that you read in isn't NULL, do the same to the right side.
If you have to write out using in-order traversal, then yeah, fseek()'ing to the middle of the file would probably be easiest. Note, though, that if the original tree is lopsided, then the one that gets recreated won't be. In the worst case, if you have a degenerate tree where every right pointer is NULL, you'll end up reading a full, balanced tree back in.