hello

I have a program which is being passed 2 lines items at a time

To hold these lines i have 2 static pointers, these are:-

static char *first_string=NULL;
static char *second_string=NULL;

These 2 lines will contain data such as:-

A1|VALETING|LIMITED
A2|COMPANY|VALET|LTD

I then use strtok to move through each delimiter

first_ptr = strtok (first_string, "|,;-. ");
second_ptr = strtok (second_string, "|,;-. ");

first_ptr=strtok(first_ptr+strlen(first_ptr)+1, "|,;-. ");
second_ptr=strtok(second_ptr+strlen(second_ptr)+1, "|,;-. ");

This works fine, up to a certain stage, and then i get the following

Arithmetic Exception - Core dump.

Can anyone help? i am baffled?

Thanks

Recommended Answers

All 7 Replies

It would be really nice if u could post the entire code or atleast the section of the code which u have been getting problems in. Pasting out snippets from here and there would not fetch much help.

BTW

first_ptr=strtok(first_ptr + strlen(first_ptr) + 1, "|,;-. ");
second_ptr=strtok(second_ptr + strlen(second_ptr) + 1, "|,;-. ");

Maybe wat u are trying to do is to make the ptr to point to the second token. Well if u are trying to do that then, wat u have done is not correct.

Try this

char str[] = "now # is the time for all # good men to come to the # aid of their country";
   char delims[] = "#";
   char *result = [B]NULL[/B];
   result = strtok( str, delims );
   while( result != [B]NULL[/B] ) {
       printf( "result is \"%s\"\n", result );
       result = strtok( [B]NULL[/B], delims );
}

Output is

result is "now "
result is " is the time for all "
result is " good men to come to the "
result is " aid of their country"

Hope it helped,
bye.

here is my code

int word_match(RULE_INFO *pRuleSharedObject)
{
 
    int (*fptr)(int); /*Needed to add fuzzy rule*/
RULE_INFO SharedObject; /*Provides structure of sent info to your code*/
static char *first_string=NULL;
static char *second_string=NULL;
char *first_ptr=NULL, *second_ptr=NULL;
float match_count=100;
int word_count=0,count, FirstTime=0; 
static unsigned int max_len=0;
static void *handle=NULL;
unsigned int MatchFound=0; 
 
if ( !(FirstTime) )
{
/* Open shared object */
if ( (handle = dlopen(RULE_SHARE_OBJECT, RTLD_LAZY)) == NULL)
return -1;
FirstTime=1;
}
 
if ( max_len < pRuleSharedObject->len )
{
max_len=pRuleSharedObject->len;
if ( !(first_string) )
{
/* Allocate memory for first_string */
if ( (first_string = (char *)malloc(max_len + 1)) == NULL )
{
strcpy(pRuleSharedObject->error, "failed to allocate memory for first_string");
return -1;
}
/* Allocate memory for second_string */
if ( (second_string = (char *)malloc(max_len + 1)) == NULL )
{
strcpy(pRuleSharedObject->error, "failed to allocate memory for second_string");
return -1;
}
}
else
{
/* Reallocate memory for first_string */
if ( (first_string=(char *)realloc(first_string, max_len + 1)) == NULL )
{
strcpy(pRuleSharedObject->error, "failed to reallocate memory for first_string");
return -1;
}
/* Reallocate memory for second_string */
if ( (second_string=(char *)realloc(second_string, max_len + 1)) == NULL )
{
strcpy(pRuleSharedObject->error, "failed to reallocate memory for second_string");
return -1;
}
}
*(first_string + max_len)='\0';
*(second_string + max_len)='\0';
}
 
/* Initialise */
memcpy (first_string, pRuleSharedObject->record_a, pRuleSharedObject->len);
memcpy (second_string, pRuleSharedObject->record_b, pRuleSharedObject->len);
fprintf(stderr,"\n\nfirst string is %s\n", first_string); 
fprintf(stderr,"second string is %s\n\n\n", second_string);
first_ptr = strtok (first_string, "|,;-. "); 
second_ptr = strtok (second_string, "|,;-. ");
 
while(first_ptr)
{
word_count++;
first_ptr=strtok(first_ptr+strlen(first_ptr)+1, "|,;-. "); 
}
while(second_ptr)
{
word_count++;
second_ptr=strtok(second_ptr+strlen(second_ptr)+1, "|,;-. ");
}
 
first_ptr = strtok (first_string, "|,;-. "); 
second_ptr = strtok (second_string, "|,;-. "); 
 
while(first_ptr != NULL )
{
count=1; 
while(second_ptr != NULL )
{ 
 
SharedObject.len=min(strlen(first_ptr),strlen(second_ptr));
SharedObject.argument=pRuleSharedObject->argument;
SharedObject.record_a=first_ptr;
SharedObject.record_b=second_ptr; 
 
if ((fptr = (int (*)())dlsym(handle, "fuzzy")) == NULL)
return -1;
 
if ( (MatchFound = (*fptr)((int) &SharedObject)) == -1 )
return -1;
 
if (MatchFound)
{
/* Match */
fprintf(stderr,"Sucessfully matched %s -- %s\n", first_ptr, second_ptr );
match_count=match_count+200;
 
second_ptr = strtok (second_string, "|,;-. ");
count=0; 
break; 
}
else
{
/* No match */
second_ptr=strtok(second_ptr+strlen(second_ptr)+1, "|,;-. " ); 
 
} 
 
}
if (count)
{
second_ptr = strtok (second_string, "|,;-. ");
count=0;
} 
first_ptr=strtok(first_ptr+strlen(first_ptr)+1, "|,;-. " );
}

Inline code tags are used when you want to have pieces of code appear within a sentence.

When posting large pieces of seperate code use code tags.

/************************************************************************
 * Function     : word_match
 * Description  : word_match
 ***********************************************************************/
int word_match(RULE_INFO *pRuleSharedObject)
{
   
    int (*fptr)(int);      /*Needed to add fuzzy rule*/
    RULE_INFO SharedObject;     /*Provides structure of sent info to your code*/
    static char *first_string=NULL;
    static char *second_string=NULL;
    char *first_ptr=NULL, *second_ptr=NULL;
    float match_count=100;
    int word_count=0,count, FirstTime=0;            
    static unsigned int max_len=0;
    static void *handle=NULL;
    unsigned int MatchFound=0; 
    char delims[]="|,;-. ";
    
    if ( !(FirstTime) )
    {
         /* Open shared object */
         if ( (handle = dlopen(RULE_SHARE_OBJECT, RTLD_LAZY)) == NULL)
             return -1;
         FirstTime=1;
    }
              
    if ( max_len < pRuleSharedObject->len )
    {
        max_len=pRuleSharedObject->len;
        if ( !(first_string) )
        {
            /* Allocate memory for first_string */
            if ( (first_string = (char *)malloc(max_len + 1)) == NULL )
            {
                strcpy(pRuleSharedObject->error, "failed to allocate memory for first_string");
                return -1;
            }
            /* Allocate memory for second_string */
            if ( (second_string = (char *)malloc(max_len + 1)) == NULL )
            {
                strcpy(pRuleSharedObject->error, "failed to allocate memory for second_string");
                return -1;
            }
        }
        else
        {
            /* Reallocate memory for first_string */
            if ( (first_string=(char *)realloc(first_string, max_len + 1)) == NULL )
            {
                strcpy(pRuleSharedObject->error, "failed to reallocate memory for first_string");
                return -1;
            }
            /* Reallocate memory for second_string */
            if ( (second_string=(char *)realloc(second_string, max_len + 1)) == NULL )
            {
                strcpy(pRuleSharedObject->error, "failed to reallocate memory for second_string");
                return -1;
            }
        }
 *(first_string + max_len)='\0';
 *(second_string + max_len)='\0';
    }
          
    /* Initialise */
    memcpy (first_string, pRuleSharedObject->record_a, pRuleSharedObject->len);
    memcpy (second_string, pRuleSharedObject->record_b, pRuleSharedObject->len);
    fprintf(stderr,"\n\nfirst string  is %s\n", first_string); 
    fprintf(stderr,"second string is %s\n\n\n", second_string);
    first_ptr  = strtok (first_string,  delims ); 
    second_ptr = strtok (second_string, delims );
    
    while(first_ptr)
    {
        word_count++;
        first_ptr=strtok(first_ptr+strlen(first_ptr)+1, delims );        
    }
    while(second_ptr)
    {
        word_count++;
        
        second_ptr=strtok(second_ptr+strlen(second_ptr)+1, delims );
        
    }
       
    first_ptr  = strtok (first_string,  delims ); 
    second_ptr = strtok (second_string, delims );     
     
    while(first_ptr != NULL )
    {
        count=1;       
        while(second_ptr != NULL )
        {   
                                  
            SharedObject.len=min(strlen(first_ptr),strlen(second_ptr));
            SharedObject.argument=pRuleSharedObject->argument;
            SharedObject.record_a=first_ptr;
            SharedObject.record_b=second_ptr;           
                              
            if ((fptr = (int (*)())dlsym(handle, "fuzzy")) == NULL)
                return -1;
          
            if ( (MatchFound = (*fptr)((int) &SharedObject)) == -1 )
                return -1;
                                  
            if (MatchFound)
            {
              /* Match */
                fprintf(stderr,"Sucessfully matched %s -- %s\n", first_ptr, second_ptr );
                match_count=match_count+200;
              
                second_ptr = strtok (second_string, delims);
                count=0;                                            
                break;   
            }
            else
            {
                /* No match */
                second_ptr=strtok(second_ptr+strlen(second_ptr)+1, delims );              
               
            }                                               
              
        }
        if (count)
        {
            second_ptr = strtok (second_string, delims);
            count=0;
        }      
        first_ptr=strtok(first_ptr+strlen(first_ptr)+1, delims );
    }

You are not calling strtok correctly.

try something like this:

void foo(char *string1, char *string2)
{
    char *first=strtok(string1,"|");
    char *second=string2(string2,"|");
    while(first!=NULL)
    {
        printf("%s\n",first);
        first=strtok(NULL,"|");
    }
    while(second!=NULL)
    {
        printf("%s\n",second);
        second=strtok(NULL,"|");    
    }
}

> char *second=string2(string2,"|");
Huh - did you mean strtok()

Also, you're not calling strtok() correctly either.
You can't strtok() two strings at the same time. Remember that NULL parameter means use where you got to last time. So you must get to the end of the first string before trying to tokenise a second string.

Use strtok_r() if you want to process two strings at the same time, since this function is re-entrant, as you pass it a 3rd parameter to record where you got to.

You are absolutely correct - you cannot use strtok() for more than one "parse" at a time.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.