Hello All
I have written tis program to read the words from a text file and simply count the number of occurrences each word has. I'm still trying to get to grips with the whole pointer and freeing up the memory afterwards principle.
Could you please check out my listing a let me know if I have sucessfuly freed up all the malloc'd memory.
Many thanks
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NOTFOUND 0
#define FOUND 1
struct TheWord *AddNodeToList(char *);
void showAllNode(struct TheWord *);
struct TheWord *FindAWord(struct TheWord *, char *);
void freeAllNode(struct TheWord *);
struct TheWord
{
struct TheWord *nextWord;
char theWord[100];
int theWordCounter;
};
int main(int args, char *argv[])
{
int readChar = 0;
char thisWordBuffer[100]={'\0'};
int thisWordCounter = 0;
struct TheWord *root = malloc(sizeof(*root));
struct TheWord *tail;
struct TheWord *incrementTheWord;
/*SETUP AND OPEN THE FILE*/
FILE *fp;
/*THE FILE CAN BE ANY TEXT FILE FOR WHICH YOU WANT TO COUNT THE NUMBER OF OCCURANCES OF WORDS*/
if((fp = fopen("I:\\C\\text.txt","r"))==NULL) {
printf("Error:Can't open file");
exit(-1);
}
/*CREATE THE ROOT NODE*/
root->nextWord = NULL;
strcpy(root->theWord,"root");
root->theWordCounter = 0;
tail = root;
/*READ THE FILE UNTI EOF*/
while((readChar = fgetc(fp))!=EOF) {
/*IF THE CHAR READ IS NOT A PART OF A WORD I.E. A-Z*/
if(readChar!=' ' && readChar!='\n' && readChar!='.' && readChar!=',') {
thisWordBuffer[thisWordCounter] = readChar;
thisWordCounter++;
}
else {
/*ADD THE TERMINATOR*/
thisWordBuffer[thisWordCounter]='\0';
/*REINITIALISE THE COUNTER*/
thisWordCounter = 0;
/*IF THE WORD IS ALREADY IN THE LIST*/
if((incrementTheWord = FindAWord(root,thisWordBuffer))!=NULL) {
/*INCREMENT THE COUNTER RATHER THAN ADD THE WORD AGAIN*/
incrementTheWord->theWordCounter++;
}
else {
/*IF THE WORD IS NOT IN LIST, ADD IT*/
tail->nextWord = AddNodeToList(thisWordBuffer);
/*KEEP THE TAIL MARKER UPTODATE*/
tail = tail->nextWord;
}
}
}
showAllNode(root);
freeAllNode(root);
free(root);
return 0;
}
struct TheWord *AddNodeToList(char *wordToAdd)
{
/*CREATE A NEW STRUCURE AND GIVE IT SOME MEM*/
struct TheWord *newNode = malloc(sizeof(*newNode));
int x = 0;
/*INITIALISE TO NULL AS THIS IS END NODE*/
newNode->nextWord = NULL;
/*ITERATE THROUGH THE WORD UNTIL '\0'*/
while(*wordToAdd) {
newNode->theWord[x] = *wordToAdd;
x++;
wordToAdd++;
}
/*ADD THE TERMINATOR - I'LL BE BACK!*/
newNode->theWord[x]='\0';
/*SET THE COUNTER TO ONE AS THIS SHOULD BE FIRST ADDITION TO THE LIST*/
newNode->theWordCounter = 1;
/*RETURN THE NODE*/
return newNode;
}
void showAllNode(struct TheWord *rootNode)
{
/*ITERATE THROUGH ALL THE NODES FROM THE GIVEN ROOT*/
while(rootNode = rootNode->nextWord) {
printf("The Word [%20s] LISTED \t\t[%d] TIME(S)\n",rootNode->theWord, rootNode->theWordCounter);
}
}
void freeAllNode(struct TheWord *rootNode)
{
struct TheWord *temp;
temp = rootNode->nextWord;
while(rootNode) {
temp = rootNode->nextWord;
free(rootNode);
rootNode = NULL;
rootNode = temp;
}
}
struct TheWord *FindAWord(struct TheWord *rootNode, char *findWord)
{
int foundStatus = NOTFOUND;
while((rootNode = rootNode->nextWord) && foundStatus == NOTFOUND) {
if(strcmp(rootNode->theWord,findWord)==0)
{
foundStatus = FOUND;
}
}
return rootNode;
}