Thread: Printing to File using BST.

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Sep 2010
    Location
    Boston, MA
    Posts
    97

    Printing to File using BST.

    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
    Last edited by omGeeK; 04-25-2011 at 07:13 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File I/O..Reading a file and printing contents
    By eeengenious in forum C Programming
    Replies: 2
    Last Post: 03-14-2011, 05:58 PM
  2. Help with printing to a file!!!!
    By jregalad in forum C Programming
    Replies: 2
    Last Post: 03-10-2011, 02:33 AM
  3. File not printing
    By xniinja in forum C Programming
    Replies: 15
    Last Post: 01-13-2011, 07:38 PM
  4. Printing Every File
    By maruf10 in forum Windows Programming
    Replies: 9
    Last Post: 03-16-2009, 08:30 PM
  5. Printing to file
    By Gravedigga in forum Windows Programming
    Replies: 0
    Last Post: 08-24-2004, 03:30 PM