The following code compiles ok but I get the general 'memory access violation' error when executing it. I believe the error occurs with the Strcpy() function when I attempt to copy a pointer variable (token) to a char variable (pat). Any way to accomplish this? Thanks for the help:
#include <sys/types.h>
# include <iostream.h>
# include <string.h>
//These two include files are mandatory.
int bmsearch(char *text,char *pat,int *no,int *pos)
{
int len=strlen(pat);
int compcount=0;
int ptct=len-1;
int i,table[256];
*no=0;
for(i=0;i<256;i++)table[i]=len;
for(i=0;i<len;i++)table[pat[i]]=len-(i+1);
while(ptct<strlen(text))
{
int count=0;
while(count<len)
{
if(text[ptct-count]!=pat[len-1-count]){compcount++;break;}
else count++;
}
if(count==len)
{
(*no)++;
*(pos+(*no)-1)=(ptct-count+1);
ptct+=len;
}
else
{ptct+=table[text[ptct-count]];}
}
return compcount;
}
/****This is a test program for the bm algorithm
************************************************/
# include <conio.h>
# include <stdio.h>
main(void)
// int argc;
// char *argv[];
{
FILE *fptrin;
char pat[100];
int no,count,pos[20],i;
int pos_ave = 0;
char txtline[140];
char txtfile[]=" ";
int length,len;
char *token;
char seps[] = " ,\t\n";
length=strlen("e003stmt.dat");
strncpy(txtfile,"e003stmt.dat",length);
if((fptrin=fopen(txtfile,"rb"))==NULL)
printf("Can't Open In File !\n");
// printf("Enter the pattern string:");
// gets(pat);
while(fgets(txtline,139,fptrin)!=NULL)
{
printf("%s\n\nTokens:\n",txtline);
/* Establish string and get the first token: */
token = strtok(txtline,seps);
while(token != NULL)
{
/* While there are tokens in "string" */
printf(" %s\n",token);
/* Get next token: */
token = strtok(NULL, seps);
}
strcpy(pat,token);
count = bmsearch(txtline,pat,&no,pos);
printf("\n\nPattern string occurred %d times",no);
printf("\n%d comparisons required",count);
printf("\nPositions of occurrence:\n");
for(i=0;i<no;i++)
{
printf("%d\n",pos[i]);
switch(i)
{
case 0:
break;
default:
pos_ave = pos_ave + pos[i];
break;
}
}
if(pos_ave!=NULL)printf("pos_ave = %d\n",(pos_ave)/i++);
printf("Thank you\n\n");
return 0;
}
}