471,573 Members | 1,717 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,573 software developers and data experts.

dictionary + binary search = counting words

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[i]);
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);
}

Nov 14 '05 #1
4 3947
sun6 <su******@op.pl> wrote:
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
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #2
On Sun, 22 Feb 2004 16:30:21 +0100, "sun6" <su******@op.pl> wrote:

[tree program snipped]

I replied to your cross-post in the learn.c-c++ group.
-leor

Leor Zolman
BD Software
le**@bdsoft.com
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
Nov 14 '05 #3
Thank You very much for Your advice, the program is now working.

Nov 14 '05 #4
"sun6" <su******@op.pl> wrote in message news:<c1**********@nemesis.news.tpi.pl>...
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[i]);
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
Nov 14 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Phil Powell | last post: by
125 posts views Thread by Raymond Hettinger | last post: by
6 posts views Thread by bear220720 | last post: by
1 post views Thread by Djam | last post: by
5 posts views Thread by JerryB | last post: by
2 posts views Thread by Berimor | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by lumer26 | last post: by
reply views Thread by lumer26 | last post: by
reply views Thread by lumer26 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.