1

So I have this code:

#include <stdio.h>
#include <stdlib.h>

struct tree
{
    char data;
    struct tree *left;
    struct tree *right;
};


int findNode(char temp[], int x, int y, char val);
struct tree *insert(int data);


struct tree *binarytree(char inorder[], char preorder[], int x, int y)
{
    static int index = 0;

    if (x > y)
        return NULL;

    struct tree *new = insert(preorder[index++]);


    if (x == y)
        return new;

    int inIndex = findNode(inorder, x, y, new->data);

    new->left = binarytree(inorder, preorder, x, inIndex - 1);
    new->right = binarytree(inorder, preorder, inIndex + 1, y);

    return new;
}

int findNode(char temp[], int x, int y, char val)
{
    int i;

    for (i = x; i <= y; i++)
    {
        if (temp[i] == val)
            return i;
    }
}


struct tree *insert(int data)
{
    struct tree *tree = (struct tree *)malloc(sizeof(struct tree));
    tree->data = data;
    tree->left = NULL;
    tree->right = NULL;

    return (tree);
}

void postorder(struct tree *tree)
{
    FILE *ofp;
    ofp = fopen("201262480.txt", "w");

    if (tree == NULL)
    {
        return;
    }
    else
    {
        postorder(tree->left);
        postorder(tree->right);
        fprintf(ofp, "%d ", tree->data);
    }

}

int main()
{
    int i = 0, j;
    int temp[100];
    char c, buffer[20];
    FILE *fp;
    fp = fopen("input.txt", "r");

    if (fp != NULL)
    {
        while (1 == fscanf(fp, "%d ", &temp[i]))
        {
            i++;
        }

        char inorder[i / 2];
        char preorder[i / 2];

        for (j = 0; j < i / 2; j++)
        {
            preorder[j] = temp[j];
            inorder[j] = temp[j + (i / 2)];
        }

        int length = sizeof(inorder) / sizeof(inorder[0]);
        struct tree *root = binarytree(inorder, preorder, 0, length - 1);
        postorder(root);
    }
    else
    {
        printf("Cannot open File!\n");
    }

    return 0;
}

And I have the input file like this:

1 2 3 4 5 6 7
3 2 4 1 6 5 7
***
1 1 2
1 1 2
***end of input***

It scans up 'til the * symbol. It processes the numbers and prints an output file. Now I have two problems.

  1. How could I scan the next line of numbers (1 1 2 and 1 1 2) and repeat the process again. what arguments could i use for a while loop?
  2. The output text file only prints the last element(this can be found in the postorder function). It should print this: 3 4 2 6 7 5 1 But instead, it only prints 1. But when I use printf, it prints the correct output.

NOTE: The whole code works. No need to edit the binary tree and stuff, it's just the scanning of integers and writing the output to a file that make me nuts.

Please help!

Jørgen R
  • 10,568
  • 7
  • 42
  • 59
  • 1
    fclose(ofp); for starters. – Jiminion Aug 16 '13 at 15:21
  • Let's say I want to loop until it finds the character *, what would be the code for that? – James Reynald Mase Aug 16 '13 at 15:28
  • 1
    If these are lines, then maybe use fgets(). fgets() reads in a line at a time. Then parse the line (look for *) and throw it out if it has one or more of them. – Jiminion Aug 16 '13 at 16:29
  • @JamesReynaldMase, here is an exhausting [answer with examples](https://stackoverflow.com/a/1836454/2186301). "End Of File" or [EOF](http://www.cplusplus.com/reference/cstdio/EOF/) is a constant defined in `stdio.h` but you can be looking for `'a'`, `'\0'` or `'*'` as well. – yulian Jul 17 '20 at 11:09

1 Answers1

0

Use a access mode instead of w:

w: Create an empty file for output operations.
Each time you call postorder() function you overwrite the 201262480.txt file. That's why you the output is 1: the last call of postorder() function overwrites 201262480.txt and writes 1 there.

a: Open file for output at the end of a file.
Using this access mode, you'll get expected output.

void postorder(struct tree *tree)
{
    FILE *ofp;
    ofp = fopen("201262480.txt", "a");  // I've changed "w" to "a". 
                                        // Now the output will be proper. 
    if (tree == NULL)
    {
    fclose(ofp);
    return;
    }
    else
    {
        postorder(tree->left);
        postorder(tree->right);
        fprintf(ofp, "%d ", tree->data);
    }
    fclose(ofp);
}

The output is expected:

3 4 2 6 7 5 1 

UPDATE:

I've modified your postorder() function so it will overwrite the output file every time you run the program.

Here is the code:

void postorder(struct tree *tree)
{
    unsigned int pointerValue = 0;  
    static int overwriteFile = 0; // The file hasn't been overwritten yet.
    FILE *ofp = NULL;
    ofp = fopen("201262480.txt", "a");

    fseek(ofp, 0L, SEEK_END);       // Moving the pointer to the end of file.
    pointerValue = ftell(ofp);          // Checking if it is not `0` (the file contains some data)
    if (pointerValue != 0) {
        if (overwriteFile == 0) {
            fclose(ofp);
            ofp = fopen("201262480.txt", "w");  // "w": Create an empty file for output operations.
            fclose(ofp);
            ofp = fopen("201262480.txt", "a");  // "a": Open file for output at the end of a file.   
            overwriteFile = 1;  // The file was overwritten. 
                                // So when you call this function next time, 
                                // it will not overwrite the file. 
                                // It only does it once when you run the program.
        }
    }

    if (tree == NULL)
    {
        return;
    }
    else
    {
        postorder(tree->left);
        postorder(tree->right);
        fprintf(ofp, "%d ", tree->data);
    }
    fclose(ofp);
}
Heng Li
  • 113
  • 9
yulian
  • 1,601
  • 3
  • 21
  • 49