Thread: Printing to File using BST.

  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.

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    What "processStu" function? I don't see one. The answer is to open the FILE before you traverse the tree and pass the FILE pointer along to the functions.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Location
    Boston, MA
    Posts
    97
    Sorry forgot to put it in there, its there now.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    A way to help callback functions like this (you pass a pointer to a function to some generic routine, and it calls your function), is to arrange it so that you can also pass a "void *userData" pointer along for the ride.

    This user data pointer can be anything (it's void*). Use it to maintain any kind of context that your processing function needs from one call to the next.

    In this example, the user data is just a FILE*
    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,stdout);
        {
          FILE *fp = fopen("file.txt","w");
          BST_Traverse (list, processStu,fp);
          fclose(fp);
        }
        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, void *userData), void *userData)
    {
    //  Statements
        _traverse (tree->root, process, userData);
        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, void *userData), void *userData)
    {
    //  Statements
    if  (root)
        {
         _traverse (root->left, process, userData);
         process   (root->dataPtr, userData);
         _traverse (root->right, process, userData);
        } // 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, void *userData)
    {
    //  Local Definitions
        STUDENT aStu;
    
    //  Statements
        aStu = *(STUDENT*)stuPtr;
        fprintf(userData, "%-25s %s\n",
               aStu.name,aStu.phone);
        return;
    }   // processStu
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Sep 2010
    Location
    Boston, MA
    Posts
    97
    Thanks helped alot man
    Last edited by omGeeK; 04-27-2011 at 11:02 AM.

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