Hi experts,
I have to do following do you have any idea about it?
Reading (and Writing) a Binary Tree from (to) a file
Storing a Tree into a file requires a function to write the Tree to the file system and a function to read the Tree back into the program. Indeed, similar read and write functions can be used for other data structures too, like Lists, Queues, Stacks, as well as complex databases.
Inside a file, each binary Tree node is stored one after another in two segments: a unique ID (TREE-ID) of the node and the node itself (val and the array child). A complete binary search Tree (consisting of 7 tree nodes) would look like the following in a file:
ID01 4 ID02 ID03
ID02 2 ID04 ID05
ID03 6 ID06 ID07
ID04 1 NULL NULL
ID05 3 NULL NULL
ID06 5 NULL NULL
ID07 7 NULL NULL
Note that the file will not contain text data but the actual binary representation of the integers and ids.
Reading and writing data to file can be accomplished by the fread and fwrite library calls. The following example shows how to read and write the value of a variable named x of type long.
fread(&x, sizeof(long), 1, fstm); // reading a long value
fwrite(&x, sizeof(long), 1, fstm); // writing a long value
fstm is the FILE pointer of the file you are reading or writing.
Using fileIO in part I
You are going to use the program called fileIO. (Remember to copy this program into your computer, since this is not a "built-in" command). fileIO breaks the file into blocks and then either writes the blocks to (with fwrite or write) or reads the blocks from (with fread or read) the file. Whether to read from or write to the file and whether to use the system calls (read and write) or the C standard Library functions (fread or fwrite) depend on the parameters you pass to the program. The program fileIO takes in the following parameters:
The file size (f), default (max) = 200MB. The syntax of the argument is f=5000000
The block size (b), default = 8192. The syntax of the argument is b=4096.
The operation. This is specified with one of the following words:
read, fread, write, or fwrite
The optional word random - meaning randomly permute the order in which blocks are read or written.
The filename. If no file name is given, a random name beginning with 213IO is used.
For example: "% fileIO f=123456789 b=8192 fwrite" would fwrite 123456789 bytes (in blocks whose size is 8192 bytes) into a newly created randomly named file.
fileIO will output the time needed to write (or read) the file.
Part I - Implementing the Reader of Binary Tree Files
Implement freadTree() in TreeIO.c: the "read" function of a Binary Tree declared in TreeIO.h.
struct Tree* freadTree (const char * const fname);
This function reads a tree stored in the file whose name is given as an argument, and returns a pointer to the tree. NULL is returned if any error is encountered
open a file stream with fname (fopen());
read all the nodes from the file, constructing the tree
close the input file.
Testing the program
Compile TreeIO.c and then link it with Tree.o (compiled from the Tree.c and Tree.h that we've provided to make the executable tree.
Read the given Binary Tree file tree0.tree in the directory trees/ (trees/tree0.tree).
(i am try ing to use fread() to read nodes from the file, and save them into an array of struct node, but o dont know how can i use fread :(