• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 294
  • Last Modified:

string tokenize


i want to tokenize a string like a.b and put the right hand side into *filenames[] and the other side into *columnnames[] but when i print the columnnames they are all just a "."   .What might be the problem?

for(m=0;m<i;m++){
atomptr2=strtok(selectarraytemp[m],".");
filenames[m]=(char*)malloc(strlen(atomptr2)+1);
strcpy(filenames[m],atomptr2);
atomptr2=(NULL,".");
columnnames[m]=(char*)malloc(strlen(atomptr2)+1);
strcpy(columnnames[m],atomptr2);
}

for(i=0;i<m-1;i++)
printf("\nfilename[%d] : %s",i,filenames[i]);

for(i=0;i<m-1;i++)
printf("\ncolumnname[%d] : %s",i,columnnames[i]);
0
btocakci
Asked:
btocakci
  • 2
1 Solution
 
ikeworkCommented:
hi btocakci,

there is an (logically) error in your code:

atomptr2=(NULL,".");

should be this:

atomptr2=strtok(NULL,".");


hope it helps :)
ike
0
 
billtouchCommented:
First, don't be afraid of the space bar. It is your friend.

Can you post the whole function that compiles clean? There are several solutions depending on how the variables are defined.

Thanks,
Bill
0
 
ikeworkCommented:
this works:


#include <stdio.h>
#include <string.h>
#include <malloc.h>

int main( int argc, char** argv )
{
      char *atomptr2;
      char selectarraytemp[3][50] = { "a.b", "ert.o", "aaaa.ppp" };
      char *filenames[3];
      char *columnnames[3];
      int m, i;

      for( m = 0; m < 3; ++m )
      {
            atomptr2 = strtok( selectarraytemp[ m ], "." );

            // if there was no point ...
            if( !atomptr2 )
            {
                  filenames[ m ] = NULL;
                  columnnames[ m ] = (char*)malloc( strlen( selectarraytemp[ m ] ) + 1 );
                  strcpy( columnnames[ m ], selectarraytemp[ m ] );
            }
            else
            {
                  filenames[ m ] = (char*)malloc( strlen( atomptr2 ) + 1 );
                  strcpy( filenames[ m ],atomptr2 );
                  atomptr2 = strtok( NULL, "." );
                  columnnames[ m ] = (char*)malloc( strlen( atomptr2 ) + 1 );
                  strcpy( columnnames[ m ], atomptr2 );
            }
      }

      for(i=0;i<3;i++)
      printf("\nfilename[%d] : %s",i,filenames[i]);

      for(i=0;i<3;i++)
      printf("\ncolumnname[%d] : %s",i,columnnames[i]);

      for( i = 0; i < 3; ++i )
      {
            free( columnnames[ i ] );
            free( filenames[ i ] );
      }

      return 0;
}


hope it helps :)
ike
0
 
btocakciAuthor Commented:
i think before being more ashamed because of not seein' what i write, i must learn to use space bars..thanx..
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now