I have to create a application which reads a textfile and counts the distinct words from it an output to a file with the number of occourences of it. ex:- the 55 , I'm 120 , Went 10 etc..
I have tried to do it to my knwladge but im now in need of a help of some one. i use a structure to store each word and a pointer to point to the structure. but my compiler (TC) says "Illegal Structure Operation at function updateword" . dev C++ says that i havent declared the "wordlist"(the pointer) please can anyone help me. i have to submit this day after tommorow. im enclosing the codes below.
Please help me.
Code:#include <stdio.h> #include <conio.h> #include <string.h> #include <stdlib.h> #define MAX_WORD 200 struct word_structure{ char name[MAX_WORD + 1]; int n; }; int listitems; extern int i,add; extern char word [MAX_WORD + 1]; enum _State { OUTSIDE_WORD, INSIDE_WORD }; typedef enum _State State; void UpdateWord(int listitems); main( int argc, char *argv[] ) { extern struct word_structure words; extern struct word_structure *wordlist; FILE *p_file; /* The input file. */ /*char word [MAX_WORD + 1]; /* The current word. */ int l_word; /* Its length. */ State state = OUTSIDE_WORD; /* Are we inside or outside a word. */ int c; /* The current character read. */ int is_delim; /* Is this character a delimiter. */ const char *delims = " \t \r`~!@#$%^&*()-=+|\[]{};:",.<>/?"; int n_total; /* Total occurrences of all words. */ listitems = 0; wordlist = &words; /* Open the input file. */ if (argc < 2) { printf ("Usage: %s <input file> ", argv[0]); return (1); } p_file = fopen (argv[1], "r"); if (p_file == NULL) { printf ("Failed to open '%s'. ", argv[1]); return (1); } /* Initialize the tree. */ /* Read the input file, character by character. */ while ((c = fgetc (p_file)) != EOF) { /* Check whether the current character is a delimiter. */ is_delim = (strchr (delims, c) != NULL); /* Act according to the current state. */ if (state == OUTSIDE_WORD) { /* We are outside a word: Ignore any delimiters, but pay attention to the case that a new word has just started. */ if (! is_delim) { word[0] = c; l_word = 1; state = INSIDE_WORD; } } else { /* We are inside a word. */ if (! is_delim) { /* Current character is not a delimiter - append it to the word. */ word[l_word] = c; l_word++; } else { /* We reached the end of the word. */ state = OUTSIDE_WORD; /* Create a new WordCount object and update the tree. */ if (l_word > 0) { word[l_word] = '\0'; UpdateWord (listitems); } } } } /* Close the input file. */ fclose (p_file); /* Go over all words in the tree and print their number of occurrences. */ for(i = 0; i<listitems; i++) { /*printf("%c : %d ", *wordlist.name, *wordlist.n);*/ printf("Im OK"); wordlist++; } return (0); } void UpdateWord(int listitems) { add = 1; for(i = 0; i<listitems; i++) { if( word == wordlist.name) { wordlist.n = *wordlist.n + 1; add = 0; break; } else { ++wordlist; } } if(add) { ++wordlist; wordlist.name = word; wordlist.n = 1; listitems++; } }



LinkBack URL
About LinkBacks



I used to be an adventurer like you... then I took an arrow to the knee.