0

I'm trying to print out a in order traversal of BST to a output file. However, I get weird symbols sometimes instead of the stdin.

I've tried allocating extra space for the char arr[] but it doesnt change anything.

EDIT: I changed up the code and tried splitting it into two parts. One, where a method assigns each node to a node array in inlevel order and returns that array. And two, a different array that writes the returned values contents to a file. This time though I'm getting 2 errors.

error: incompatible types when assigning to type ‘struct bstNode *’ from type ‘bstNode’ error: incompatible types when assigning to type ‘struct bstNode *’ from type ‘bstNode’

void traverseTree(bstNode *root){
        FILE *file;
        file = fopen("newFile", "w");
        char *arr;
        arr = (char*)malloc(10000 * sizeof(char));
        if(root == NULL)
                return;

        traverseTree(root->left);
        fgets(arr, 100, stdin);
        fprintf(file, arr);
        traverseTree(root->right);

}

New code below

bstNode* traverseTree(bstNode *root, int i){

        bstNode *arr[100];
        if(root == NULL)
                return;

        traverseTree(root->left, i);

        arr[i] = root;
        ++i;
        traverseTree(root->right, i);

 return *arr;
}

void writeToFile(bstNode *root, char *outputFileName){
        FILE *file;
        file = fopen("newFile", "w");
        bstNode *arr = traverseTree(root, 0);
        int i = 0;
        root = arr[i];
        while(root!= NULL){
        fprintf(file, root->data);
        ++i;
        root = arr[i];
        }

}
Cris Manrique
  • 83
  • 1
  • 7
  • 4
    You have a recursive function that tries to open the same file every time its called. It makes no sense. – Tom Karzes Jun 24 '19 at 01:14
  • You might be more clear about what the error looks like. In any case, you have a memory leak for not `free`-ing the `malloc`-ed array again. Far better is using an array with local storage duration: `char arr[10000];`, that one cleans up automatically. If you want to further improve: have the array just once, either as static variable or by setting it up in a non-recursive wrapper function and passing it to the recursive worker function as argument. – Aconcagua Jun 24 '19 at 01:18
  • And if using `malloc`: You should *always* check the result for being `NULL`, otherwise you risk undefined behaviour (possibly resulting in crash). – Aconcagua Jun 24 '19 at 01:19
  • [You should not cast the return value for malloc](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – Ed Heal Jun 24 '19 at 01:25

1 Answers1

0

Apart from the following

  • Opening the same file repeatedly
  • Not checking the return values from fopen and malloc
  • Not requiring the cast from malloc
  • Not checking the return value from fgets

The following line

fprintf(file, arr);

has a problem is arr has a % symbol in it (as this is for formatting - see the manual page)

Use the following

fprintf(file, "%s", arr);

instead.

EDIT

After your edit, another problem:

return *arr;

This is returning data that is on the stack - this will go out of scope.

Also still got the same problem with fprintf - please read the manual page for this

Ed Heal
  • 59,252
  • 17
  • 87
  • 127