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];
}
}