-2

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;

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.

MY code:

#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
#include<time.h>
#include<pthread.h>
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->key1 = item;
    temp1->left1 = temp1->right1 = NULL;
    return temp1;
}

// A utility function to do inorder traversal of BST
void inorder(struct node1 *root1)
{
    if (root1 != NULL)
    {
        inorder(root1->left1);
        printf("%d ", root1->key1);
        inorder(root1->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 < node1->key1)
        node1->left1  = insert1(node1->left1, key1);
    else
        node1->right1 = insert1(node1->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->left1 != NULL)
        current = current->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 < root1->key1)
        root1->left1 = deletenode1(root1->left1, key1);
    // If the key1 to be deleted is greater than the root1's key1,
    // then it lies in right1 subtree
    else if (key1 > root1->key1)
        root1->right1 = deletenode1(root1->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->left1 == NULL)
        {
            struct node1 *temp1 = root1->right1;
            free(root1);
            return temp1;
        }
        else if (root1->right1 == NULL)
        {
            struct node1 *temp1 = root1->left1;
            free(root1);
            return temp1;
        }
        // node1 with two children: Get the inorder successor (smallest
        // in the right1 subtree)
        struct node1* temp1 = minValuenode1(root1->right1);
        // Copy the inorder successor's content to this node1
        root1->key1 = temp1->key1;
        // Delete the inorder successor
        root1->right1 = deletenode1(root1->right1, temp1->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->left = temp->right = NULL;
        temp->data = val;
        *tree = temp;
        return;
    }
    if(val < (*tree)->data)
    {
        insert(&(*tree)->left, val);
    }
    else if(val > (*tree)->data)
    {
        insert(&(*tree)->right, val);
    }
}
void print_preorder(node * tree)
{
    if (tree)
    {
        printf("%d\n",tree->data);
        print_preorder(tree->left);
        print_preorder(tree->right);
    }

}
void print_inorder(node * tree)
{
    if (tree)
    {
        print_inorder(tree->left);
        printf("%d\n",tree->data);
        print_inorder(tree->right); // i want to print this as a tree
    }
}
void print_postorder(node * tree)
{
    if (tree)
    {
        print_postorder(tree->left);
        print_postorder(tree->right);
        printf("%d\n",tree->data);
    }
}
void deltree(node * tree)
{
    if (tree)
    {
        deltree(tree->left);
        deltree(tree->right);
        free(tree);
    }
}
node* search(node ** tree, int val)
{
    if(!(*tree))
    {
        return NULL;
    }
    if(val < (*tree)->data)
    {
        search(&((*tree)->left), val);
    }
    else if(val > (*tree)->data)
    {
        search(&((*tree)->right), val);
    }
    else if(val == (*tree)->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", &option);
    clock_t tic = clock();
    node *root;
    node *tmp;
    //int i;
    root = NULL;
    struct node1 *root1 = NULL;
    /* Inserting nodes into tree */
    while (scanf("%d", &a) && a!='q'){insert(&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", &searcharg);
            tmp = search(&root, searcharg);
    if (tmp)
    {
        printf("The searched node is present = %d\n", tmp->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", &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();
}

3 Answers3

1

Solution 1: Open the file in your main function and pass file pointer as parameter into every function you need to write to the file. Close it at the end of the main function.

Solution 2: Use global variable:

FILE * g_my_file = fopen("my_file.txt", "wt");

Then instead of your printf use:

fprintf(g_my_file, "%d\n", tree->data);

Maybe close it somewhere at the end of the program/main:

fclose(g_my_file);
-1

If your are running the program on linux just type:

./a.out >> your_filename
my_name
  • 87
  • 1
  • 1
  • 11
-1

As this is not mentioned in other answers you can open file with proper mode

Demo

   fp = fopen("file.txt","a");      
   //write dome data here this will be appended to the end of the file  
   fclose(fp);

Also this does not need global variable for file identifier

Edit: With proper mode a in fopen, there is no need for fseek

hessam hedieh
  • 780
  • 5
  • 18