Solved

strtok help

Posted on 2001-07-10
7
653 Views
Last Modified: 2008-02-01
if i have

char szText[] = "test?blah?123";

and i want to use strtok to seperate the text on the ? and i want to assign each segment to a 100 byte buffer in

char *pszBuffer;
pszBuffer = malloc (10 * 100);

how would i do it with strtok? i cant figure it out.
0
Comment
Question by:Kitty__Kong
7 Comments
 
LVL 2

Accepted Solution

by:
MadYugoslav earned 50 total points
ID: 6268384
char szText[] = "test?blah?123";char seps[]   = " ,\t\n";
char *token;
char *pszBuffer;

void GetLines( void )
{
   int i=0;
   pszBuffer = malloc (10 * 100);
   token = strtok( szText, "?" );
   while( token != NULL )
   {
      strcpy(pszBuffer[i*100], token
      i++;
      token = strtok( NULL, "?" );
   }
}

0
 
LVL 10

Expert Comment

by:makerp
ID: 6268551
here is a function i wrote a while back to strtok and return a array of pointers, it aint perfect and probably could be improved but it works

/* function to take a string and split it up into parts separted by
   seperator . returns a pointer to pointers !! */
char **splitText(const char *text, int *count, char* seperator)
{
     char **lines = NULL;
     char *ptr = NULL, *temp;
     int size = 0;
     /* set the count to one just in case */
     *count = 0;
     temp = (char*)malloc(strlen(text) + 1);
     if(!temp)
     {
          return NULL;
     }
     strcpy(temp,text);
     ptr = strtok(temp,seperator);
     while(ptr != NULL)
     {
          if(lines)
          {
                    size = _msize(lines);
          }
          lines = (char**)realloc(lines, size + sizeof(char*));
          if(lines)
          {
               lines[*count] = (char*)malloc(strlen(ptr) + 1);
               if(lines[*count])
               {
                    strcpy(lines[*count],ptr);
                    *count = *count + 1;
               }
          }
          ptr = strtok(NULL,seperator);
     }
     free(temp);
     return lines;
}
0
 
LVL 2

Expert Comment

by:MadYugoslav
ID: 6268569
I think that my comment also can be improved but work.
Is it ?
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 10

Expert Comment

by:makerp
ID: 6268726
to print out the values after calling simply

int count = 0;

char **retval = splitText(my_str,&count,"?")

for(int i=0;i<count;i++)
{
  printf("%s\n",retval[i])
}

then to free all of the values in the pointer array simply call this function passing the array and the count

i.e.

pm_free_ptr_2_ptr((void**)retval,count);

/* clean up a ** list of pointers */
void pm_free_ptr_2_ptr(void **ptr, int count)
{
     if(ptr)
     {    
          for(int i = 0;i < count;i++)
          {
               if(ptr[i])
               {
                    free(ptr[i]);
               }
          }
          free(ptr);
     }
}

to allocate each in a 100 bytes buffer simply replace

lines[*count] = (char*)malloc(strlen(ptr) + 1);
 with
lines[*count] = (char*)malloc(100);

although using just strcpy is dangerous now as your token may be greater then 100? so instead of

strcpy(lines[*count],ptr);
 use
strncpylines[*count],ptr,100);

paul

0
 
LVL 10

Expert Comment

by:makerp
ID: 6268730
strncpy(lines[*count],ptr,100);

whoops, missing (
0
 
LVL 1

Author Comment

by:Kitty__Kong
ID: 6269989
strcpy(pszBuffer[i*100], token)

is causing some warnings

D:\Programming\c_c++\strings\strings.c(19) : warning C4047: 'function' : 'char *' differs in levels of indirection from 'char '


D:\Programming\c_c++\strings\strings.c(19) : warning C4024: 'strcpy' : different types for formal and actual parameter 1
0
 

Expert Comment

by:wkmatt42
ID: 6270069
Try

strcpy(&pszBuffer[i*100], token)

or

strcpy(pszBuffer + i*100, token)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question