I am working on an assignment to count specific words in a file. I am able to copy the file into an structure of an array. I thought I would use strcmp and compare each member of the structure to the others, increase my count if they are equal etc.
I ran into a snag becuase the strcmp function works with character pointers, and not structures.
Any advice would be helpful. I am new to C and programing so I hope the code is easy to follow. The UniqueWord function at the bottom is where I am running into the problem with strcmp not working with the structure.
Thanks
Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#define MAXWORDS 1000
typedef struct
{
char Word[88];
}UniqueWords;
char *GetWord(char *Source, char *Dest);
UniqueWords WordCount(UniqueWords Source[1000]);
int main(int c, char **v)
{
char *LocationGetty = "C:\\Users\\svjefcoat\\Documents\\Visual Studio 2010\\Projects\\assignment 10\\Getty.txt";
UniqueWords Word[MAXWORDS];
UniqueWords WordCopy[MAXWORDS];
UniqueWords *pList;
char line[99];
char Dest[39];
char *Read;
FILE *pStorage;
char *pDArray;
pStorage = fopen(LocationGetty, "r"); //Location Getty
if(!pStorage)
return 0;
pList = Word;
while(1)
{
if(feof(pStorage))
break;
*line=NULL;
fgets(line,sizeof(line), pStorage);
pDArray = malloc(sizeof(line));
strcpy(pDArray, line);
pDArray[(strlen(pDArray))+1] = '\0';
printf("%-s", line);
Read = pDArray;
for(;;)
{
Read = GetWord(Read, pList->Word);
if(*pList->Word=='\0')
break;
pList++;
}
}
WordCount(Word);
free(pDArray);
fclose(pStorage);
getchar();
return 0;
}
char *GetWord(char *Source, char *Dest)
{
// skip all leading space characters
while(*Source)
{
if(isspace(*Source))
{
Source++;
continue;
}
else
break;
}
while(*Source)
{
if(isspace(*Source))
{
*Dest = '\0';
return Source;
}
*Dest++ = *Source++;
}
*Dest = '\0';
return Source;
}
UniqueWords WordCount(UniqueWords Source[1000])
{
UniqueWords *pSource;
UniqueWords *pSourceTest;
int i;
char *topSource;
char *topSourceTest;
pSource=Source;
pSourceTest=Source;
i=0;
for(;;)
{
if(*pSourceTest->Word=='\0')
break;
if(strcmp(pSource, pSourceTest))
{
i ++;
}
pSourceTest ++;
}
}