Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

advancing a token

Status
Not open for further replies.

darfader

Programmer
Aug 6, 2003
38
GB
Code:
int process_record(char * current_record)
{
char * buffer;
char activity_month[BUFSIZ];
char current_month[BUFSIZ];
int fields = 0;
time_t curtime;
struct tm *loctime;
char record[BUFSIZ];
int loop, count = 0;

   curtime = time(NULL);
   loctime = localtime(&curtime);
   strftime(current_month, BUFSIZ, "%Y%m", loctime);

   strcpy(record, current_record);
   count = strlen(record);

      for(loop = 0; loop < count; loop++)
      {
         buffer =  strtok(record, &quot;;&quot;);

         if(buffer != NULL)
         {
            fields++;

            if(fields == DATE_DELIM && buffer == NULL)
            {
               error_func(&quot;In get_activity_month().&quot;, &quot;Error : Activity_Month has no data&quot;, SYS | FATAL);
            }
            else if(fields == DATE_DELIM)
            {
               strncpy(activity_month, buffer, 6);
            }
printf(&quot;count %d\n&quot;, count);
printf(&quot;loop %d\n&quot;, loop);
printf(&quot;fields %d\n&quot;, fields);
printf(&quot;constant %d\n&quot;, DATE_DELIM);
printf(&quot;original record [%s]\n&quot;, record);
printf(&quot;whats in the box [%s]\n&quot;, buffer);
printf(&quot;start date [%d]\n&quot;, start_date);
printf(&quot;end date [%d]\n&quot;, end_date);
printf(&quot;month date [%s]\n&quot;, activity_month);

            ...


/* OUTPUT */
count 480
loop 78
fields 79
constant 79
original record [United Kingdom]
whats in the box [United Kingdom]
start date [200209]
end date [200309]
month date [United]
count 494
loop 78
fields 79
constant 79
original record [United Kingdom]
whats in the box [United Kingdom]
start date [200209]
end date [200309]
month date [United]
count 487
loop 78
fields 79
constant 79
original record [United Kingdom]
whats in the box [United Kingdom]
start date [200209]
end date [200309]
month date [United]


In the above snippet, what do I need to do to move along the record ?


What's happening is the first field is being captured for each record rather than a field further along the line and it's because I am not stepping through the record properly,

I have tried this
Code:
               while(buffer != NULL)
               {
                  buffer = strtok(NULL, &quot;;&quot;);
               }

but without success


tia,
 
a single record

United Kingdom;EUMA;Europe + Middle East + Africa;GBR;GB;EL;Equant LNO;1078314.00;LGWTRUS2;108324F;2449800;3033.00;1S;LGW11S-23; ; ; ;X25;1116.00;ANUK820113;BT;P; ; ;0.00;9.60;;19980316.00;;;SABRENC;LGW;LGW1; ;28.27;45.18;0.00;0.00;0.00;0.00;0.00;0.00;0.00;0.00;; ;;0.00;0.00;0.00;0.00;0.00;0.00;0.00;0.00;0.00;0.00;0.00;0.00;0.00;0.00;0.00;0.00;20020710.00;20030610.00;SITA;50.00;Valids;GBP;0.62;20030624.00;28.27;45.18;0.00;0.00;0.00;0.00;28.27;45.18;20030501.00;;;;SI0001015

the delimiters are semi-colons

the 79th field has the following value - 20030501.00

I would like to capture either

1) first 6 chars
or
2) the whole field
or
3) everything in the record after the 79th delimiter

which ever is simplest, fastest etc,



tia,
 
How about
Code:
#include <stdio.h>
#include <string.h>

char *find_field ( char *line, char delim, int count ) {
    char    *p = line;
    int     i;
    for ( i = 0 ; i < count && p != NULL ; i++ ) {
        p = strchr( p, delim ); /* find it */
        if ( p != NULL ) p++;   /* skip it */
    }
    return p;
}

int main(int argc, char * argv[])
{
    /* example record exactly as would be returned by fgets() */
    /* as if reading from a file */
    char test[] = &quot;United Kingdom;EUMA;Europe + Middle East + Africa;GBR;GB;EL;&quot;
                  &quot;Equant LNO;1078314.00;LGWTRUS2;108324F;2449800;3033.00;1S;LG&quot;
                  &quot;W11S-23; ; ; ;X25;1116.00;ANUK820113;BT;P; ; ;0.00;9.60;;199&quot;
                  &quot;80316.00;;;SABRENC;LGW;LGW1; ;28.27;45.18;0.00;0.00;0.00;0.0&quot;
                  &quot;0;0.00;0.00;0.00;0.00;; ;;0.00;0.00;0.00;0.00;0.00;0.00;0.00&quot;
                  &quot;;0.00;0.00;0.00;0.00;0.00;0.00;0.00;0.00;0.00;20020710.00;20&quot;
                  &quot;030610.00;SITA;50.00;Valids;GBP;0.62;20030624.00;28.27;45.18&quot;
                  &quot;;0.00;0.00;0.00;0.00;28.27;45.18;20030501.00;;;;SI0001015\n&quot;;
    char *res = find_field( test, ';', 79 );
    if ( res != NULL ) {
        /* use strcpy or whatever on the non-NULL result */
        printf( &quot;%.6s\n&quot;, res );    /* print the first 6 chars */
    } else {
        /* too few delimiters in line - bad record? */
    }
    return 0;
}

--
 
so you don't like strtok() either :)


thank you very much Salem, this is just fine....


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top