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);
}
"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);
}