dictionary + binary search = counting words

Discussion in 'C Programming' started by sun6, Feb 22, 2004.

  1. sun6

    sun6 Guest

    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);
    }
     
    sun6, Feb 22, 2004
    #1
    1. Advertising

  2. sun6

    -berlin.de Guest

    sun6 <> 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 ___ -berlin.de
    \__________________________ http://www.toerring.de
     
    -berlin.de, Feb 22, 2004
    #2
    1. Advertising

  3. sun6

    Leor Zolman Guest

    On Sun, 22 Feb 2004 16:30:21 +0100, "sun6" <> wrote:

    [tree program snipped]

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

    Leor Zolman
    BD Software

    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
     
    Leor Zolman, Feb 22, 2004
    #3
  4. sun6

    sun6 Guest

    Thank You very much for Your advice, the program is now working.
     
    sun6, Feb 22, 2004
    #4
  5. sun6

    Michael Str. Guest

    "sun6" <> wrote in message news:<c1ai2k$gsg$>...
    > 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
     
    Michael Str., Feb 22, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Francois
    Replies:
    13
    Views:
    624
    Will Hartung
    Oct 14, 2004
  2. Gaz
    Replies:
    2
    Views:
    1,620
  3. Replies:
    6
    Views:
    5,769
    Simon
    May 20, 2005
  4. Bogdan

    Binary tree search vs Binary search

    Bogdan, Oct 18, 2010, in forum: C Programming
    Replies:
    22
    Views:
    3,232
    Michael Angelo Ravera
    Oct 21, 2010
  5. edwardfredriks

    counting up instead of counting down

    edwardfredriks, Sep 6, 2005, in forum: Javascript
    Replies:
    6
    Views:
    232
    Dr John Stockton
    Sep 7, 2005
Loading...

Share This Page