Kitty__Kong
asked on
strtok help
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I think that my comment also can be improved but work.
Is it ?
Is it ?
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,cou nt);
/* 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,1 00);
paul
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**)
/* 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,1
paul
strncpy(lines[*count],ptr, 100);
whoops, missing (
whoops, missing (
ASKER
strcpy(pszBuffer[i*100], token)
is causing some warnings
D:\Programming\c_c++\strin gs\strings .c(19) : warning C4047: 'function' : 'char *' differs in levels of indirection from 'char '
D:\Programming\c_c++\strin gs\strings .c(19) : warning C4024: 'strcpy' : different types for formal and actual parameter 1
is causing some warnings
D:\Programming\c_c++\strin
D:\Programming\c_c++\strin
Try
strcpy(&pszBuffer[i*100], token)
or
strcpy(pszBuffer + i*100, token)
strcpy(&pszBuffer[i*100], token)
or
strcpy(pszBuffer + i*100, token)
/* 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)
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;
}