Thread: file pointers + recursion = sore head

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    54

    Unhappy file pointers + recursion = sore head

    heres my little problem, i need to save some information to a file that is stored in a binary tree, getting the information is not a problem, <gotta love recursion aint you>, now to the problem, the code for opening the file is within the function and obviously it keeps opening the file with every item from the binary tree, so whats the problem i hear you ask, well i've already got stuff in the file that i've loading into the program and when i save that stuff and some new stuff i want to be able to erase the contents and start from fresh, but with every pass with recursion the new info gets erased also, i cant put the file open routine outside of the function cause the function will still call it every time

    heres the code

    void saveContents(PTRnode* &root, char pathname[60])
    {
    FILE *fp; //file pointer

    int temp;
    if(root==NULL)
    {
    return;
    }
    else
    {
    fp = fopen(pathname, "w");
    fprintf(fp,"%s\n%s\n%d\n",root->data.artist, root->data.title, root->data.numberTracks);
    for(temp=1;temp<=root->data.numberTracks;temp++)
    {
    fprintf(fp,"%s\n",&root->data.SongList[temp]);
    }
    fclose(fp);
    saveContents(root->left, pathname);
    saveContents(root->right, pathname);
    }


    }


    i hope that made sense all that typing, hope someone can help me

    thanx in advance korbitz

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Simple, open the file first, and instead of passing 'path name', pass it the file pointer. Additionally, why are you using '&root' here? Anyway...

    void saveContents(PTRnode* root, FILE *fp )

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    54
    hey that worked thanx mate

    on the &root thingy, i'm positive i tried the other way and it never worked properly so i use * &root and it works fine

    relatively inexperienced at c, but slowly getting there

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Assuming the following:

    typedef struct node {
    ...node stuff...
    } NODE;

    Then you can do the following:

    NODE *nptr; //pointer to a node

    Assuming the following function:

    void function( NODE *npt, FILE *fp )
    { ...stuff... }

    Then a function call:

    function( nptr, myFilePtr );


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    54
    i think that looks fimiliar to what i had before, but for the life of me i could not get the address of root and assign a NULL value to it, thats why i changed it, inexperience probably

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Can't figure out problem with code
    By Beast() in forum C Programming
    Replies: 4
    Last Post: 04-16-2005, 05:27 PM