my code
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
char *pch;
char *parameters[4];
int paramcount=0;
char b[]="mani,raju,musa,kumar";
pch=strtok(b,",");
while (pch != NULL)
{
//parameters[paramcount]=pch;
parameters[paramcount]=(char*)malloc(strlen(pch) + 1);
strcpy(parameters[paramcount],pch);
paramcount++;
pch = strtok (NULL, ",");
}//while (pch != NULL)
for(int t=0;t<4;t++)
printf("\n%s\n",parameters[t]);
}
the above code works fine.when i change the input as follows
char b[]="mani,,,kumar"
The output is not correct.
How to free the memory allocated?
how to handle strtok when there is no value between delimiter
Regards,
Manikandan
int main(void)
{
char *pch;
char *parameters[4];
int paramcount = 0;
char b[] = "mani,,,kumar";
pch = strtok(b,",");
while (pch != NULL)
{
if(strlen(pch))
{
parameters[paramcount] = malloc(strlen(pch) + 1);
strcpy(parameters[paramcount++], pch);
pch = strtok (NULL, ",");
}
}
for(int t=0;t< paramcount;t++)
{
printf("\n%s\n",parameters[t]);
}
while(--paramcount >= 0)
{
printf("\t%s\n",parameters[paramcount]);
free(parameters[paramcount]);
}
return 0;
}
--
==============
Not a pedant
==============
Don't use strtok, which has various usage nuisances. Try toksplit,
below.
Don't forget to read my sig below and the URLs referenced.
/* ------- file toksplit.h ----------*/
#ifndef H_toksplit_h
# define H_toksplit_h
# ifdef __cplusplus
extern "C" {
# endif
#include <stddef.h>
/* copy over the next token from an input string, after
skipping leading blanks (or other whitespace?). The
token is terminated by the first appearance of tokchar,
or by the end of the source string.
The caller must supply sufficient space in token to
receive any token, Otherwise tokens will be truncated.
Returns: a pointer past the terminating tokchar.
This will happily return an infinity of empty tokens if
called with src pointing to the end of a string. Tokens
will never include a copy of tokchar.
released to Public Domain, by C.B. Falconer.
Published 2006-02-20. Attribution appreciated.
*/
const char *toksplit(const char *src, /* Source of tokens */
char tokchar, /* token delimiting char */
char *token, /* receiver of parsed token */
size_t lgh); /* length token can receive */
/* not including final '\0' */
# ifdef __cplusplus
}
# endif
#endif
/* ------- end file toksplit.h ----------*/
/* ------- file toksplit.c ----------*/
#include "toksplit.h"
/* copy over the next token from an input string, after
skipping leading blanks (or other whitespace?). The
token is terminated by the first appearance of tokchar,
or by the end of the source string.
The caller must supply sufficient space in token to
receive any token, Otherwise tokens will be truncated.
Returns: a pointer past the terminating tokchar.
This will happily return an infinity of empty tokens if
called with src pointing to the end of a string. Tokens
will never include a copy of tokchar.
A better name would be "strtkn", except that is reserved
for the system namespace. Change to that at your risk.
released to Public Domain, by C.B. Falconer.
Published 2006-02-20. Attribution appreciated.
*/
const char *toksplit(const char *src, /* Source of tokens */
char tokchar, /* token delimiting char */
char *token, /* receiver of parsed token */
size_t lgh) /* length token can receive */
/* not including final '\0' */
{
if (src) {
while (' ' == *src) *src++;
while (*src && (tokchar != *src)) {
if (lgh) {
*token++ = *src;
--lgh;
}
src++;
}
if (*src && (tokchar == *src)) src++;
}
*token = '\0';
return src;
} /* toksplit */
#ifdef TESTING
#include <stdio.h>
#define ABRsize 6 /* length of acceptable token abbreviations */
int main(void)
{
char teststring[] = "This is a test, ,, abbrev, more";
const char *t, *s = teststring;
int i;
char token[ABRsize + 1];
puts(teststring);
t = s;
for (i = 0; i < 4; i++) {
t = toksplit(t, ',', token, ABRsize);
putchar(i + '1'); putchar(':');
puts(token);
}
puts("\nHow to detect 'no more tokens'");
t = s; i = 0;
while (*t) {
t = toksplit(t, ',', token, 3);
putchar(i + '1'); putchar(':');
puts(token);
i++;
}
puts("\nUsing blanks as token delimiters");
t = s; i = 0;
while (*t) {
t = toksplit(t, ' ', token, ABRsize);
putchar(i + '1'); putchar(':');
puts(token);
i++;
}
return 0;
} /* main */
#endif
/* ------- end file toksplit.c ----------*/
--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
Try strsep. It's not ANSI, but it is available in GNU and many other
implementations, and does exactly what you want.
~Glynne
snip
>#include<stdio.h>
>#include<string.h>
>#include<stdlib.h>
>
>int main(void)
>{
>
> char *pch;
> char *parameters[4];
> int paramcount = 0;
>
> char b[] = "mani,,,kumar";
>
> pch = strtok(b,",");
>
> while (pch != NULL)
> {
> if(strlen(pch))
If pch is not NULL, then this if will always evaluate true. strtok
skips over any delimiters before the first non-delimiter.
> {
> parameters[paramcount] = malloc(strlen(pch) + 1);
>
> strcpy(parameters[paramcount++], pch);
>
> pch = strtok (NULL, ",");
> }
> }
>
> for(int t=0;t< paramcount;t++)
Most C89 compilers won't support this type of definition.
> {
> printf("\n%s\n",parameters[t]);
> }
>
> while(--paramcount >= 0)
> {
> printf("\t%s\n",parameters[paramcount]);
>
What is this love affair with useless vertical white space?
> free(parameters[paramcount]);
> }
>
> return 0;
>}
Remove del for email
> I need to split the value stored in a string and store them to another
> charrecter array.I am using strtok function.But i am getting invalid
> output when there is no value between delimiter
strtok() has at least these problems:
* It merges adjacent delimiters. If you use a comma as your
delimiter, then "a,,b,c" will be divided into three tokens,
not four. This is often the wrong thing to do. In fact, it
is only the right thing to do, in my experience, when the
delimiter set contains white space (for dividing a string
into "words") or it is known in advance that there will be
no adjacent delimiters.
* The identity of the delimiter is lost, because it is
changed to a null terminator.
* It modifies the string that it tokenizes. This is bad
because it forces you to make a copy of the string if
you want to use it later. It also means that you can't
tokenize a string literal with it; this is not
necessarily something you'd want to do all the time but
it is surprising.
* It can only be used once at a time. If a sequence of
strtok() calls is ongoing and another one is started,
the state of the first one is lost. This isn't a
problem for small programs but it is easy to lose track
of such things in hierarchies of nested functions in
large programs. In other words, strtok() breaks
encapsulation.
--
"Am I missing something?"
--Dan Pop
All of which are met by the toksplit which I published here earlier
in the thread.