I am trying to build a binary Tree of integers from a file in C for college, and my program returns error exit code 255 on windows
my code is as follows
// this is a program for building a binary tree and
// visiting its nodes using preOrder travsersal
#include < stdio.h>
#include < string.h>
// define a structure for the data
typedef struct{
int num;
}NodeData;
// define the treenode
typedef struct treeNode{
NodeData data;
struct treeNode *left,*right;
}TreeNode,*TreeNodePtr;
// define the BinaryTree
typedef struct{
TreeNodePtr root;
}BinaryTree;
main(){
TreeNodePtr buildTree(FILE *);
void preOrder(TreeNodePtr);
// open the file
FILE *in=fopen("integers.txt","r");
if(in!=NULL){
// declare the binarytree
BinaryTree bt;
// build the tree
bt.root=buildTree(in);
printf("\n the preOrder traversal is:");
preOrder(bt.root);
} else printf("Cannot Open the file");
} // end of the main
TreeNodePtr buildTree(FILE *in){
int num;
fscanf (in, "%d", num);
printf ("number %d \n", num);
TreeNodePtr p = (TreeNodePtr) malloc(sizeof(TreeNode));
p -> data.num;
printf("root %d \n", p);
p->left=buildTree(in);
printf("left %d \n", p->left->data.num);
p->right=buildTree(in);
printf("right %d \n",p->right->data.num);
return p;
} //end of build tree
void visit(TreeNodePtr node){
printf("%d", node->data.num);
}
void preOrder(TreeNodePtr node){
if(node !=NULL){
visit(node);
preOrder(node->left);
preOrder(node->right);
}
}
On a side note how do i format code so that it looks somewhat decent on the forums?
@Eden has found most of your issues. Your main program doesn't return and doesn't have a type. According to the C11 standard these are the only valid main definitions:
int main(void); int main(int argc, char* argv[]);
Just a friendly word of advice. You should try to download and install and IDE of some kind for you programming class. Using print statements for debugging purposes is absolutely awful (no matter what your dumb professors might say). There are plenty of good free IDEs out there that are cross platform, but if you are on windows then nothing beats visual studio. The 2017 version was just released, and the community edition of it is free to use for academic use (assuming you also have a Microsoft account of some kind to login with as it is required with that version). Part of learning to programming is learning to use the tools (especially in native development land).