[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

5.4

how to use fread function to read binary tree nides from the file

Asked by zahmadian in C Programming Language

Tags: c, fread, binary, read

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 :(
 
Related Solutions
Keywords: how to use fread function to read bina…
 
Loading Advertisement...
 
[+][-]10/12/05 04:29 PM, ID: 15073616Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/13/05 01:24 AM, ID: 15075347Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/13/05 05:34 AM, ID: 15076419Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/13/05 06:34 AM, ID: 15076868Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/13/05 06:43 AM, ID: 15076931Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/13/05 06:56 AM, ID: 15077020Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/13/05 08:08 AM, ID: 15077762Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/13/05 11:10 AM, ID: 15079653Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/13/05 11:16 AM, ID: 15079705Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/13/05 11:28 AM, ID: 15079857Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/13/05 11:52 AM, ID: 15080082Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/13/05 12:01 PM, ID: 15080185Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/13/05 01:57 PM, ID: 15081234Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/13/05 02:21 PM, ID: 15081416Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/13/05 03:51 PM, ID: 15081910Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/13/05 03:57 PM, ID: 15081939Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/13/05 04:23 PM, ID: 15082055Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/13/05 04:27 PM, ID: 15082067Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/13/05 04:33 PM, ID: 15082095Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/13/05 04:43 PM, ID: 15082145Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/13/05 08:46 PM, ID: 15082899Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/13/05 10:08 PM, ID: 15083110Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/14/05 02:27 AM, ID: 15083949Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zone: C Programming Language
Tags: c, fread, binary, read
Sign Up Now!
Solution Provided By: cwwkie
Participating Experts: 2
Solution Grade: A
 
[+][-]10/14/05 07:23 AM, ID: 15085632Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/14/05 02:15 PM, ID: 15089075Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/15/05 01:53 AM, ID: 15090675Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/15/05 05:44 AM, ID: 15091093Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091111-EE-VQP-89