dictionary + binary search = counting words

S

sun6

this is a program counting words from "text_in.txt" file and writing them in
"text_out.txt". it uses binary tree search, but there is an error when i use
insert ()
thanks for any help


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

FILE *text_in; // file in
FILE *text_out; //file out


typedef struct DICTIONARY *TREE;
struct DICTIONARY {
char *word;
int count;
TREE leftChild, rightChild;
};


TREE t;


/*functions which will be used later*/
TREE makeempty(TREE t);
TREE scan_file();
void memory_error(void);
void print_tree(TREE T);
char *save_string(char *string);
TREE insert(char *word_from_file, TREE T);
/* end*/


int main(int argc, char *argv[])

{

char terminate;

text_in = fopen("text_in.txt", "r");
text_out = fopen("text_out.txt", "w");


if(text_in == NULL) {
printf("Error: Couldn't open text_in.txt\n");
scanf("%s", &terminate);
exit(8);
}
if(text_out == NULL) {
printf("Error: Couldn't open pliku text_out.txt\n");
scanf("%s", &terminate);
exit(8);
}

t = makeempty(t);

printf("entering scan_file()\n");

t = scan_file();
print_tree(t);

fclose(text_out);

scanf("%s", &terminate);
return(0);

}



/***************************************************************
* memory_error - *
***************************************************************/
void memory_error(void)
{
fprintf(stderr, "Blad:Brak pamieci\n");
exit(8);
}



/***************************************************************
* empty tree*
***************************************************************/

TREE makeempty(TREE t)

{
if (t!=NULL) {
makeempty(t->leftChild);
makeempty(t->rightChild);
free(t);
}

return NULL;
}




/********************************************************
* scan_file -- finding words in a file*
********************************************************/
TREE scan_file()
{
printf("jestem w scan()\n\n");
char word[100];
char *word_ptr = NULL;
int index;
int ch;
int i;
int counter = 1;


while (1) {
while (1) {
ch = fgetc(text_in);
printf("petla I: ch = %d - lit %c\n", ch, ch);
if (isalpha(ch) || (ch == EOF))
break;
}

if (ch == EOF)
break;

word[0] = ch;
for (index = 1; index < sizeof(word); ++index) {
ch = fgetc(text_in);
counter++;
printf("petla II: ch %d - lit %c\n", ch, ch);
if (!isalpha(ch))
break;
word[index] = ch;
}
word[index] = '\0';


for(i = 0; i < counter; i++)
printf("%c", word);
counter = 1;

printf("\n\npointer *word_ptr = %d\n", word_ptr);
word_ptr = save_string(word);
printf("pointer *word_ptr after doing save_string = %s\n",
word_ptr);

t = insert(word_ptr, t);
printf("word on tree\n\n");

}
return t;
fclose(text_in);
}



TREE insert(char *word_from_file, TREE T)
{
int result;
printf("puttin the word on tree\n");
result = strcmp(T->word, word_from_file); /*** ERROR :( ***/
if (T == NULL) {
T = (TREE) malloc(sizeof(struct DICTIONARY));
T->word = word_from_file;
T->leftChild = NULL;
T->rightChild = NULL;
T->count = 1;
}


else if (result > 0)
T->leftChild = insert(word_from_file, T->leftChild);
else if (result < 0)
T->rightChild = insert(word_from_file, T->rightChild);
else if (result == 0)
T->count++;
return T;
}




void print_tree(TREE T)
{
printf("drukuje drzewo do pliku\n");
if (T == NULL)
return;

print_tree(T->leftChild);
fprintf(text_out,"%4d %s\n", T->count, T->word);
print_tree(T->rightChild);
}




char *save_string(char *string)
{
char *new_string;

new_string = malloc((unsigned) (strlen(string) + 1));

if (new_string == NULL)
memory_error();

strcpy(new_string, string);
return (new_string);
}
 
J

Jens.Toerring

sun6 said:
this is a program counting words from "text_in.txt" file and writing them in
"text_out.txt". it uses binary tree search, but there is an error when i use
insert ()

I just looked at the line you marked, there may other problems in your
program...
TREE insert(char *word_from_file, TREE T)
{
int result;
printf("puttin the word on tree\n");
result = strcmp(T->word, word_from_file); /*** ERROR :( ***/

If T is still NULL (which you test for only in the next line) you
can't dereference it, so T->word will lead to a segmentation fault.
if (T == NULL) {
T = (TREE) malloc(sizeof(struct DICTIONARY));

There's no need for the cast, it is often recommended to write the
allocation like this

T = malloc( sizeof *T );

And you should check if malloc() was successful before you use T.
T->word = word_from_file;

Regards, Jens
 
M

Michael Str.

sun6 said:
this is a program counting words from "text_in.txt" file and writing them in
"text_out.txt". it uses binary tree search, but there is an error when i use
insert ()
thanks for any help


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

FILE *text_in; // file in
FILE *text_out; //file out


typedef struct DICTIONARY *TREE;
struct DICTIONARY {
char *word;
int count;
TREE leftChild, rightChild;
};


TREE t;


/*functions which will be used later*/
TREE makeempty(TREE t);
TREE scan_file();
void memory_error(void);
void print_tree(TREE T);
char *save_string(char *string);
TREE insert(char *word_from_file, TREE T);
/* end*/


int main(int argc, char *argv[])

{

char terminate;

text_in = fopen("text_in.txt", "r");
text_out = fopen("text_out.txt", "w");


if(text_in == NULL) {
printf("Error: Couldn't open text_in.txt\n");
scanf("%s", &terminate);
exit(8);
}
if(text_out == NULL) {
printf("Error: Couldn't open pliku text_out.txt\n");
scanf("%s", &terminate);
exit(8);
}

t = makeempty(t);

printf("entering scan_file()\n");

t = scan_file();
print_tree(t);

fclose(text_out);

scanf("%s", &terminate);
return(0);

}



/***************************************************************
* memory_error - *
***************************************************************/
void memory_error(void)
{
fprintf(stderr, "Blad:Brak pamieci\n");
exit(8);
}



/***************************************************************
* empty tree*
***************************************************************/

TREE makeempty(TREE t)

{
if (t!=NULL) {
makeempty(t->leftChild);
makeempty(t->rightChild);
free(t);
}

return NULL;
}




/********************************************************
* scan_file -- finding words in a file*
********************************************************/
TREE scan_file()
{
printf("jestem w scan()\n\n");
char word[100];
char *word_ptr = NULL;
int index;
int ch;
int i;
int counter = 1;


while (1) {
while (1) {
ch = fgetc(text_in);
printf("petla I: ch = %d - lit %c\n", ch, ch);
if (isalpha(ch) || (ch == EOF))
break;
}

if (ch == EOF)
break;

word[0] = ch;
for (index = 1; index < sizeof(word); ++index) {
ch = fgetc(text_in);
counter++;
printf("petla II: ch %d - lit %c\n", ch, ch);
if (!isalpha(ch))
break;
word[index] = ch;
}
word[index] = '\0';


for(i = 0; i < counter; i++)
printf("%c", word);
counter = 1;

printf("\n\npointer *word_ptr = %d\n", word_ptr);
word_ptr = save_string(word);
printf("pointer *word_ptr after doing save_string = %s\n",
word_ptr);

t = insert(word_ptr, t);
printf("word on tree\n\n");

}
return t;
fclose(text_in);
}



TREE insert(char *word_from_file, TREE T)
{
int result;
printf("puttin the word on tree\n");
result = strcmp(T->word, word_from_file); /*** ERROR :( ***/


Check here both passing parameters before call "strcmp":
if( ( T->word != NULL )&& (word_from_file != NULL) )
.....

If it is Ok, may be you have to check if these are not empty strings:
if( ( T->word[0] != '\0' )&& ( word_from_file[0] != '\0' ) )
Depends if "strcmp" works with empty strings.

It should be a good taste to do such kind of self testing especially
during development stage.

if (T == NULL) {
T = (TREE) malloc(sizeof(struct DICTIONARY));
T->word = word_from_file;
T->leftChild = NULL;
T->rightChild = NULL;
T->count = 1;
}


else if (result > 0)
T->leftChild = insert(word_from_file, T->leftChild);
else if (result < 0)
T->rightChild = insert(word_from_file, T->rightChild);
else if (result == 0)
T->count++;
return T;
}




void print_tree(TREE T)
{
printf("drukuje drzewo do pliku\n");
if (T == NULL)
return;

print_tree(T->leftChild);
fprintf(text_out,"%4d %s\n", T->count, T->word);
print_tree(T->rightChild);
}




char *save_string(char *string)
{
char *new_string;

new_string = malloc((unsigned) (strlen(string) + 1));

if (new_string == NULL)
memory_error();

strcpy(new_string, string);
return (new_string);
}


regards
Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top