C language how to print to file without overwriting it in a recurssive function? : <p>I have multiple functions that are recursive. They print multiple times the results. I need to create a file that contains all those outputs.
The functions that print are:void print_inorder,void print_preorder, void print pre_order;</p>

<p>EXAMPLE INPUT: 1 (case option-preorder), 1 2 3 4 5 q(the numbers that are inserted into the tree, q ends the scanf);
OUTPUT: 1 2 3 4 5, I need this to be printed into a file too.</p>

<p>MY code:</p>

<pre><code>#include&lt;stdlib.h&gt;
#include&lt;stdio.h&gt;
#include&lt;conio.h&gt;
#include&lt;time.h&gt;
#include&lt;pthread.h&gt;
struct node1
{
    int key1;
    struct node1 *left1, *right1;
};

// A utility function to create a new BST node1
struct node1 *newnode1(int item)
{
    struct node1 *temp1 =  (struct node1 *)malloc(sizeof(struct node1));
    temp1-&gt;key1 = item;
    temp1-&gt;left1 = temp1-&gt;right1 = NULL;
    return temp1;
}

// A utility function to do inorder traversal of BST
void inorder(struct node1 *root1)
{
    if (root1 != NULL)
    {
        inorder(root1-&gt;left1);
        printf("%d ", root1-&gt;key1);
        inorder(root1-&gt;right1);
    }
}

/* A utility function to insert1 a new node1 with given key1 in BST */
struct node1* insert1(struct node1* node1, int key1)
{
    /* If the tree is empty, return a new node1 */
    if (node1 == NULL) return newnode1(key1);

    /* Otherwise, recur down the tree */
    if (key1 &lt; node1-&gt;key1)
        node1-&gt;left1  = insert1(node1-&gt;left1, key1);
    else
        node1-&gt;right1 = insert1(node1-&gt;right1, key1);

    /* return the (unchanged) node1 pointer */
    return node1;
}
/* Given a non-empty binary search tree, return the node1 with minimum
   key1 value found in that tree. Note that the entire tree does not
   need to be searched. */
struct node1 * minValuenode1(struct node1* node1)
{
    struct node1* current = node1;

    /* loop down to find the left1most leaf */
    while (current-&gt;left1 != NULL)
        current = current-&gt;left1;

    return current;
}
/* Given a binary search tree and a key1, this function deletes the key1
   and returns the new root1 */
struct node1* deletenode1(struct node1* root1, int key1)
{
    // base case
    if (root1 == NULL) return root1;
    // If the key1 to be deleted is smaller than the root1's key1,
    // then it lies in left1 subtree
    if (key1 &lt; root1-&gt;key1)
        root1-&gt;left1 = deletenode1(root1-&gt;left1, key1);
    // If the key1 to be deleted is greater than the root1's key1,
    // then it lies in right1 subtree
    else if (key1 &gt; root1-&gt;key1)
        root1-&gt;right1 = deletenode1(root1-&gt;right1, key1);
    // if key1 is same as root1's key1, then This is the node1
    // to be deleted
    else
    {
        // node1 with only one child or no child
        if (root1-&gt;left1 == NULL)
        {
            struct node1 *temp1 = root1-&gt;right1;
            free(root1);
            return temp1;
        }
        else if (root1-&gt;right1 == NULL)
        {
            struct node1 *temp1 = root1-&gt;left1;
            free(root1);
            return temp1;
        }
        // node1 with two children: Get the inorder successor (smallest
        // in the right1 subtree)
        struct node1* temp1 = minValuenode1(root1-&gt;right1);
        // Copy the inorder successor's content to this node1
        root1-&gt;key1 = temp1-&gt;key1;
        // Delete the inorder successor
        root1-&gt;right1 = deletenode1(root1-&gt;right1, temp1-&gt;key1);
    }
    return root1;
}
struct bin_tree {
int data;
struct bin_tree * right, * left;
};
typedef struct bin_tree node;
void insert(node ** tree, int val)
{
    node *temp = NULL;
    if(!(*tree))
    {
        temp = (node *)malloc(sizeof(node));
        temp-&gt;left = temp-&gt;right = NULL;
        temp-&gt;data = val;
        *tree = temp;
        return;
    }
    if(val &lt; (*tree)-&gt;data)
    {
        insert(&amp;(*tree)-&gt;left, val);
    }
    else if(val &gt; (*tree)-&gt;data)
    {
        insert(&amp;(*tree)-&gt;right, val);
    }
}
void print_preorder(node * tree)
{
    if (tree)
    {
        printf("%d\n",tree-&gt;data);
        print_preorder(tree-&gt;left);
        print_preorder(tree-&gt;right);
    }

}
void print_inorder(node * tree)
{
    if (tree)
    {
        print_inorder(tree-&gt;left);
        printf("%d\n",tree-&gt;data);
        print_inorder(tree-&gt;right); // i want to print this as a tree
    }
}
void print_postorder(node * tree)
{
    if (tree)
    {
        print_postorder(tree-&gt;left);
        print_postorder(tree-&gt;right);
        printf("%d\n",tree-&gt;data);
    }
}
void deltree(node * tree)
{
    if (tree)
    {
        deltree(tree-&gt;left);
        deltree(tree-&gt;right);
        free(tree);
    }
}
node* search(node ** tree, int val)
{
    if(!(*tree))
    {
        return NULL;
    }
    if(val &lt; (*tree)-&gt;data)
    {
        search(&amp;((*tree)-&gt;left), val);
    }
    else if(val &gt; (*tree)-&gt;data)
    {
        search(&amp;((*tree)-&gt;right), val);
    }
    else if(val == (*tree)-&gt;data)
    {
        return *tree;
    }
}
void main()
{
    int a;
    int searcharg;
    int deletearg;
    char option;
printf("1:PreOrder\n2:InOrder\n3:PostOrder\n4:search\n5:Delete Node\nYour Option: ");
scanf("%c", &amp;option);
    clock_t tic = clock();
    node *root;
    node *tmp;
    //int i;
    root = NULL;
    struct node1 *root1 = NULL;
    /* Inserting nodes into tree */
    while (scanf("%d", &amp;a) &amp;&amp; a!='q'){insert(&amp;root,a); root1 = insert1(root1, a);}
          // these

    /* Printing nodes of tree */
    switch(option)
    {
        case '1':
        printf("Pre Order Display\n");
    print_preorder(root); // i want to print this as a tree
    break;
        case '2':
             printf("In Order Display\n");
    print_inorder(root); // i want to print this as a tree
break;
        case '3':
            printf("Post Order Display\n");
    print_postorder(root); // i want to print this as a tree
    break;

        case '4':
            scanf("%d", &amp;searcharg);
            tmp = search(&amp;root, searcharg);
    if (tmp)
    {
        printf("The searched node is present = %d\n", tmp-&gt;data);
    }
    else
    {
        printf("Data Not found in tree.\n");
    }
    break;
    default:
            printf("Error! operator is not correct\n");
            break;
        case '5':
       //     printf("scan");
       //     scanf("%d", &amp;deletearg);
            root1 = deletenode1(root1, 5);
            inorder(root1);
        }
      // i want to print this as a tree
    /* Deleting all nodes of tree */
    deltree(root);
    clock_t toc = clock();
    printf("\nElapsed: %f seconds\n", (double)(toc - tic) / CLOCKS_PER_SEC);
    getch();
}
</code></pre>
