I am getting a run time error (This program has performed an illegal action and will be shut down) I have narrowed it down to the approximate area where fread is called to get the contents of a file and begin the search for a word.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 200
static int cmp( char *a, char *b, int len);
static char *search( char *text, char *word, int len);
int main(int argc, char *argv[])
{
int i,x=1;
char *buf,*found;
FILE *fd;
if (argc<2)
{
printf("Usage: WORD <file> <word>\n");
return 1;
}
strcat(argv[1], ".txt");
if ((fd=fopen(argv[1], "r+")) == NULL)
{
printf("File execution failure\n");
return 1;
}
while ( (fread(buf, sizeof(char),MAX ,fd )) != 0)
{
while ( (found = search(buf, argv[2], strlen(argv[2]))) != NULL)
printf("%d) %s",x++,*found);
}
printf("\n\nDone\n");
fclose(fd);
return 0;
}
static int cmp( char *a, char *b, int len)
{
for (; *a == *b; a++, b++)
{
if (--len==0)
return 0;
}
return 1;
}
static char *search( char *text, char *word, int len)
{
for (; *text != '\0'; text++)
{
if ( cmp(word,text,len) == 0)
return text;
}
return NULL;
}