ok so i made those changes.........
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void createUp();
void createDown();
void showMenu();
void createRight();
void createLeft();
void search();
void listWords();
void searchWord();
int createBinaryTree();
long fileSize(FILE *);
struct tree
{
char *word; //The word to be stored in a node
// struct tree *left; //Left child of the node
struct tree *right; //Right child of the node
int count; //How many of these words are found by now
};
FILE *fp; //File Pointer
struct tree *node;
struct tree *sptr;
struct tree *q;
char *insdata;
char fileName[1000];
int main(void)
{
//Create the Binary Search Tree
if (createBinaryTree() == 0) //Show Menu
{
showMenu();
}
}
void listWords()
{
struct tree *temp;
temp = (struct tree *)malloc(sizeof(struct tree));
temp = node;
while(temp->right!=NULL)
{
printf("\n\tWord:\t%s\t\t\t\tCount:\t%d",temp->word,temp->count);
temp=temp->right;
}
temp = node;
getchar();
free(temp);
}
void showMenu()
{
char ch = 'a';
while(ch!='q')
{
printf("\n\t\t---------------------------------");
printf("\n\t\t| Binary Search Tree |");
printf("\n\t\t---------------------------------");
printf("\n\t\t|\tl - List all words\t|");
printf("\n\t\t|\ts - Search a words\t|");
printf("\n\t\t|\tq - Quit\t\t|");
printf("\n\t\t---------------------------------");
printf("\n\t\t| Press your choise (l,s,q)\t|");
printf("\n\t\t---------------------------------\n\t\t\t\t");
ch = getchar();
switch(ch)
{
case 'l':
sptr=node;
listWords();
break;
case 's':
sptr=node;
searchWord();
break;
case 'q':
free(node);
free(sptr);
free(q);
default:
break;
}
}
}
void searchWord()
{
int check=1;
char *searchWord;
printf("\n\n\t\tPlease enter the word to search: ");
// gets(searchWord);
while(sptr->right !=NULL)
{
if (strcmp(searchWord,sptr->word) == 0) {
printf("\n\n\t\tWord Occurances found:%d\n",sptr->count);
getchar();
check = 0;
break;
}
sptr=sptr->right; //Send sptr to its right
}
if (strcmp(searchWord,sptr->word) == 0 && check !=0) {
printf("\n\n\t\tWord Occurances found:%d\n",(*sptr).count);
getchar();
check = 0;
}
if(check ==1) {
printf("\n\n\t\tNo occurances of this word found.");
getchar();
}
}
int createBinaryTree()
{
char fd[1024]; //Textual Data Pointer
char *token;
int check = 0;
int counter = 1;
int true = 1;
printf("\n\t\t---------------------------------------");
printf("\n\t\t------- Frequency of Words ---------");
printf("\n\t\t---------------------------------------");
printf("\n\n\t\tPlease enter the text file name : ");
gets(fileName);
printf("\n\t\t---------------------------------------");
printf("\n\n\t\t\tReading file");
// delay(500);
printf(".");
// delay(100);
printf(".");
// delay(100);
printf(".");
// delay(100);
printf(".");
// delay(100);
if ((fp = fopen(fileName, "r")) ==NULL){
printf("\n\n\t\t\tError reading file.");
getchar();
return 1;
}
else {
fgets(fd, fileSize(fp) + 1, fp);
fclose(fp); //Close the file
token = strtok(fd," ");
if (token == NULL) {
printf("\n\n\t\tThe text file is empty.");
true = 0; //So that loop is not entered
getchar();
return 1; //Don't show the menu
}
else {
node = (struct tree *)malloc(sizeof(struct tree));
(*node).word = token;
(*node).count = 1;
node->right = NULL;
sptr=node;
q=node;
// loop until finishied
while (true)
{
check = 0;
// extract string from string sequence
token = strtok(NULL, " ");
if (token == NULL)
{
true = 0; //Break the loop
}
insdata = token;
// printf("\nCounter %d\n",counter);
// puts(insdata);
counter++;
sptr= q;
while(sptr->right !=NULL && check==0)
{
if (strcmp(insdata,sptr->word) == 0) {
(*sptr).count++; //Increment
// printf("%d",(*sptr).count);
check=1;
}
sptr=sptr->right; //Send sptr to its right
}
if (strcmp(insdata,sptr->word) == 0) {
(*sptr).count++; //Increment
// printf("%d",(*sptr).count);
check=1;
}
if (check !=1) {
createDown();
}
sptr = node;
printf("%d",true);
}
return 0;
}
}
}
long fileSize(FILE *stream)
{
long curpos, length;
curpos = ftell(stream);
fseek(stream, 0L, SEEK_END);
length = ftell(stream);
fseek(stream, curpos, SEEK_SET);
return length;
}
void createDown()
{
struct tree *temp;
temp = (struct tree *)malloc(sizeof(struct tree));
temp->word = insdata;
(*temp).count = 1;
temp->right= NULL;
sptr->right=temp;
}
/*
void createRight()
{
if(sptr->right==NULL) {
// cout<<" "<<insdata<<" IS THE RIGHT child of "<<q->word<<endl;
sptr->right= malloc(sizeof(q));
sptr=sptr->right;
sptr->word=insdata;
sptr->count = 1;
sptr->left=NULL;
sptr->right=NULL;
q=node;
}
else {
if(strcmp(insdata,sptr->word) > 0)
{
sptr=sptr->right;
q=sptr;
if(strcmp(insdata,sptr->word) > 0)
createRight();
else
createUp();
}
else {
sptr=sptr->left;
q=sptr;
createRight();
}
}
}
void createLeft()
{
if(sptr->left==NULL)
{
// cout<<" "<<insdata<<" IS THE LEFT child of "<<q->word<<endl;
sptr->left=malloc(sizeof(q));
sptr=sptr->left;
sptr->word=insdata;
sptr->count = 1;
sptr->right=NULL;
sptr->left=NULL;
q=node;
}
else
{
if(strcmp(insdata,sptr->word) < 0)
{
sptr=sptr->left;
q=sptr;
if(strcmp(insdata,sptr->word) > 0)
createRight();
else
createLeft();
}
else
{
sptr=sptr->right;
q=sptr;
createRight();
}
}
}
void search()
{
sptr=node;
while(sptr!=NULL)
{
//This commented section will work when we will have to insert equal
data
if(strcmp(insdata,sptr->word) == 0)
{
printf("\nThis is not insertable");
printf("\nInsert child ");
cin>>insdata;
search();
break;
}
else
{
if(strcmp(insdata,sptr->word) > 0)
sptr=sptr->right;
else
sptr=sptr->left;
// }
}
sptr=node;
}
void createUp()
{
struct tree *temp = malloc(sizeof(node));
temp->word = insdata;
temp->count = 1;
temp->right= sptr;
node = temp;
}
*/
and when i complied it......i got this error:
[bmhm0008@munro ctec202]$ gcc tutorial.c -o tuo
/tmp/cc6enaBa.o(.text+0x30c): In function `createBinaryTree':
: the `gets' function is dangerous and should not be used.
but i went ahead and ran the program......and i got the menu and all
and i typed the file name i wanted to search
[bmhm0008@munro ctec202]$ tuo
---------------------------------------
------- Frequency of Words ---------
---------------------------------------
Please enter the text file name : Hello.txt
---------------------------------------
Segmentation fault
ok so i took some stuff out
here is what i got now......
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void createUp();
void createDown();
void showMenu();
void createRight();
void createLeft();
void search();
void listWords();
void searchWord();
int createBinaryTree();
long fileSize(FILE *);
struct tree
{
char *word; //The word to be stored in a node
// struct tree *left; //Left child of the node
struct tree *right; //Right child of the node
int count; //How many of these words are found by now
};
FILE *fp; //File Pointer
struct tree *node;
struct tree *sptr;
struct tree *q;
char *insdata;
//> char *fileName;
char fileName[1000];
int main(void)
{
//Create the Binary Search Tree
if (createBinaryTree() == 0) //Show Menu
{
showMenu();
}
}
void listWords()
{
struct tree *temp;
temp = (struct tree *)malloc(sizeof(struct tree));
temp = node;
while(temp->right!=NULL)
{
printf("\n\tWord:\t%s\t\t\t\tCount:\t%d",temp->word,temp->count);
temp=temp->right;
}
temp = node;
getchar();
free(temp);
}
void showMenu()
{
char ch = 'a';
while(ch!='q')
{
printf("\n\t\t---------------------------------");
printf("\n\t\t| Binary Search Tree |");
printf("\n\t\t---------------------------------");
printf("\n\t\t|\tl - List all words\t|");
printf("\n\t\t|\ts - Search a words\t|");
printf("\n\t\t|\tq - Quit\t\t|");
printf("\n\t\t---------------------------------");
printf("\n\t\t| Press your choise (l,s,q)\t|");
printf("\n\t\t---------------------------------\n\t\t\t\t");
ch = getchar();
switch(ch)
{
case 'l':
sptr=node;
listWords();
break;
case 's':
sptr=node;
searchWord();
break;
case 'q':
free(node);
free(sptr);
free(q);
default:
break;
}
}
}
void searchWord()
{
int check=1;
char *searchWord;
printf("\n\n\t\tPlease enter the word to search: ");
// gets(searchWord);
while(sptr->right !=NULL)
{
if (strcmp(searchWord,sptr->word) == 0) {
printf("\n\n\t\tWord Occurances found:%d\n",sptr->count);
getchar();
check = 0;
break;
}
sptr=sptr->right; //Send sptr to its right
}
if (strcmp(searchWord,sptr->word) == 0 && check !=0) {
printf("\n\n\t\tWord Occurances found:%d\n",(*sptr).count);
getchar();
check = 0;
}
if(check ==1) {
printf("\n\n\t\tNo occurances of this word found.");
getchar();
}
}
int createBinaryTree()
{
char fd[1024]; //Textual Data Pointer
char *token;
int check = 0;
int counter = 1;
int true = 1;
printf("\n\t\t---------------------------------------");
printf("\n\t\t------- Frequency of Words ---------");
printf("\n\t\t---------------------------------------");
printf("\n\n\t\tPlease enter the text file name : ");
gets(fileName);
Try those two changes. I didn't try them.