So, I just started Binary Search Trees in class this week, and this is part of a sample program to print student names to the stdout. For my program that I have to do I must print to a file, now I have tried a lot of times and the problem is that when I create a file in my "processStu" function is there a way to keep the file open and not reopen or close everytime the function is called. Or should make a loop somehow? I'm stuck here, I'm thinking of writing a new function using completely different logic just want to know if i can do it this way somehow and save a lot of time.
-- In the sample all it calls is the printList function once to print the entire tree in inorder --
Regards
omGeeK
Code:
/* ==================== printList ======================
Prints the phone book.
Pre list has been created (may be null)
Post printed
*/
void printList (BST_TREE* list)
{
// Statements
printf("\n*Name List*\n");
BST_Traverse (list, processStu);
printf("*End of List*\n");
return;
} // printList
/* =================== BST_Traverse ===================
Process tree using inorder traversal.
Pre Tree has been created (may be null)
process ÒvisitsÓ nodes during traversal
Post Nodes processed in LNR (inorder) sequence
*/
void BST_Traverse (BST_TREE* tree,
void (*process) (void* dataPtr))
{
// Statements
_traverse (tree->root, process);
return;
} // end BST_Traverse
/* =================== _traverse ===================
Inorder tree traversal. To process a node, we use
the function passed when traversal was called.
Pre Tree has been created (may be null)
Post All nodes processed
*/
void _traverse (NODE* root,
void (*process) (void* dataPtr))
{
// Statements
if (root)
{
_traverse (root->left, process);
process (root->dataPtr);
_traverse (root->right, process);
} // if
return;
} // _traverse
/* =================== processStu =====================
Print one student's data.
Pre stu is a pointer to a student
Post data printed and line advanced
*/
void processStu (void* stuPtr)
{
// Local Definitions
STUDENT aStu;
// Statements
aStu = *(STUDENT*)stuPtr;
printf("%-25s %s\n",
aStu.name,aStu.phone);
return;
} // processStu