ok, friends, according to ur advices i changed the code to follows
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;
int i,add;
char word [MAX_WORD + 1];
struct word_structure words;
struct word_structure *wordlist;
enum _state
{
OUTSIDE_WORD,
INSIDE_WORD
};
typedef enum _state State;
void UpdateWord();
main( int argc, char *argv[] )
{
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\n\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>\n", argv[0]);
return (1);
}
p_file = fopen (argv[1], "r");
if (p_file == NULL)
{
printf ("Failed to open '%s'.\n", 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 ();
}
}
}
}
/* 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\n", *wordlist.name, *wordlist.n);*/
printf("Im OK");
wordlist++;
}
return (0);
}
void UpdateWord()
{
add = 1;
for(i = 0; i<listitems; i++)
{
if( wordlist.name = word)
{
wordlist.n = *wordlist.n + 1;
add = 0;
break;
}
else
{
++wordlist;
}
}
if(add)
{
++wordlist;
strcpy(wordlist.name,word);
/*wordlist.name = word;*/
wordlist.n = 1;
listitems++;
}
}
i removed the passing of the "listitems" its a global varible. please guide me.