larry12720 0 Newbie Poster

keep on getting this error after I put my input file, the program compiles but when I try to use it it gave me Core segmentation error.
I guess there is something wrong with the memory. plz help

Goal: My program should take input string from a file and do a binary search tree.
What I did was that create a link-list to get the data from input file. And use that to create a binary tree.

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

typedef struct list nodes;//The structure type declaraction
typedef struct bintree tree; //The structure type 

struct list
{
  char item[15]; //The character of the data.
  struct list *next; //pointer to the next node.
};

struct bintree //creating a binary search tree.
{
  char treeitem[15]; //The character to the binary tree.
  struct bintree *leftnode;
  struct bintree *rightnode;
};

void createlinklist(nodes *pt, FILE *infile1, FILE *outfile1);
void createbintree(tree *treept, nodes *temp, FILE *outfile2);

int main(int argc, char *argv[])
{
  nodes *begin = (nodes *)malloc(sizeof(nodes));
  //The pointer to the beginning of the linked-list.

  tree *start=(tree *)malloc(sizeof(tree));
  //The pointer to the beginning of theb binary search tree.
  
  FILE *infile; //input file
  FILE *outfile; //output file

  int count = 3; //The number define argv.
  if(count != argc)
    {
      fprintf(stderr, "bad command number lines\n");
      exit(1);
    }
  else
    {
      if((infile = fopen(argv[1], "r"))==NULL)
	{
	  fprintf(stderr, "cannot open file\n");
	  exit(1);
	}
      //remember to do the same for output file
      else
	{
	  infile = fopen(argv[2], "r");
	  outfile =fopen(argv[3], "w");

	  createlinklist(begin, infile, outfile);
	  createbintree(start, begin, outfile);
	  fclose(infile);
	  fclose(outfile);
	}
    }
}

void createlinklist(nodes *pt, FILE *infile1, FILE *outfile1)
{
  char data[15];
  int counter =0;
  while(fgets(data, 15, infile1)) //read it until end file
    {
      strcpy(pt->item, data); //Copy the data in to linked-list.
      pt->next = (nodes *)malloc(sizeof(nodes)); //allocating new memory for new
      counter++;
      createlinklist(pt->next, infile1, outfile1); //recursively do it again.
      
    }
  fscanf(outfile1, "%d %s", &counter, 
	 " is the number of strings in input file\n"); 
}
     
void createbintree(tree *treepc, nodes *temp, FILE *outfile2)
{
  int countleft = 0;
  int countright = 0;
  int countleftleav =0, countrightleav =0, totalleav;
  int counttree = 0;
  while(temp->item)
    {
      if(strcmp(temp->item, temp->next->item) > 0) //If this is greater than 0, goes to the leftnode of the tree
	{
	  strcpy(treepc->treeitem, temp->item); //Copy the data in linked-list to binary search tree
	  treepc->leftnode = (tree *)malloc(sizeof(tree));
	  if(treepc->leftnode != NULL)
	    {
	      countleftleav++;
	    }
	  countleft++;
	  createbintree(treepc->leftnode, temp, outfile2);
	}
      else
	{
	  strcpy(treepc->treeitem, temp->item);
	  treepc->rightnode = (tree *)malloc(sizeof(tree));
	  if(treepc->rightnode != NULL)
	    {
	      countrightleav++;
	    }
	  countright++;
	  createbintree(treepc->rightnode, temp, outfile2);
	}
      temp = temp->next;
    }
  if(countleft > countright)
    {
      counttree = countleft;
    }
  else
    {
      counttree = countright;
    }
  totalleav = countleftleav + countrightleav;
  fscanf(outfile2, "%d %s", &counttree, " Is the height of the tree\n");
  fscanf(outfile2, "%d %s", &totalleav, " Is the leaves in binary tree\n");
  fscanf(outfile2, "%d %s", &countleft, " The height of the left subtree\n");
  fscanf(outfile2, "%d %s", &countleftleav, 
	 "The number of string in left tree\n");
  fscanf(outfile2, "%d %s", &countright, " The height of the right subtree\n");
  fscanf(outfile2, "%d %s", &countrightleav, 
	 "The number of string in right tree\n");

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.