473,320 Members | 2,004 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Help with strtok

Hi
I am writing a Program
in which i get input as

#C1012,S,A#C1013,S,U

I want to get C1012,S,A using strtok and then pass this to function
CreateVideo
which will further strtok this (C1012,S,A) and store the required
values.

Now here is the piece of that code:

#define DELIM2 #
char * field;
char fieldcopy[20];

/*Here i have input as #C1012,S,A#C1013,S,U*/
field = strtok(NULL,DELIM2);
while(field != NULL)
{
strcpy(fieldcopy,field);
CreateCopies(copy,fieldcopy,NoCopies);
field = strtok(NULL,DELIM2);
printf("Field in CreateVideo is %s\n",field);
}

Now if I call CreateCopies the strtok doesn't tokenize till the end.
But if i comment the CreateCopies call it does tokenize till the end.
In the first case the second time i call strtok field gets a value of
NULL but works fine if i dont call CreateObjects.

Why this behaviour???

Mar 14 '06 #1
8 2222
On 2006-03-14, ma***********@gmail.com <ma***********@gmail.com> wrote:
Hi
I am writing a Program
in which i get input as

#C1012,S,A#C1013,S,U

I want to get C1012,S,A using strtok and then pass this to function
CreateVideo
which will further strtok this (C1012,S,A) and store the required
values.

Now here is the piece of that code:

#define DELIM2 #
you should change this DELIM2 to use a string.
char * field;
char fieldcopy[20];

/*Here i have input as #C1012,S,A#C1013,S,U*/
field = strtok(NULL,DELIM2);
Its not clear if you have called strtok with your field as first
argument before.

while(field != NULL)
{
strcpy(fieldcopy,field);
CreateCopies(copy,fieldcopy,NoCopies);
field = strtok(NULL,DELIM2);
printf("Field in CreateVideo is %s\n",field);
}

Now if I call CreateCopies the strtok doesn't tokenize till the end.
But if i comment the CreateCopies call it does tokenize till the end.
In the first case the second time i call strtok field gets a value of
NULL but works fine if i dont call CreateObjects.

Why this behaviour???


You should post all the code, theres not enough info there :
specifically the set up of "field" and the initial call to strtok which
should have "field" as the first argument. Since from the look of it,
and assuming nothing nasty, Createfields doesnt change "field" then
the *current* problem is nothing to do with Createfields : could be some bad
pointers because strtok wasnt initialised properly.
Mar 14 '06 #2
"ma***********@gmail.com" <ma***********@gmail.com> wrote:
field = strtok(NULL,DELIM2);
while(field != NULL)
{
strcpy(fieldcopy,field);
CreateCopies(copy,fieldcopy,NoCopies);
field = strtok(NULL,DELIM2);
printf("Field in CreateVideo is %s\n",field);
}

Now if I call CreateCopies the strtok doesn't tokenize till the end.
But if i comment the CreateCopies call it does tokenize till the end.


Does CreateCopies perhaps also use strtok()? You can't nest uses of
strtok(), because it keeps only a single, static state.

Richard
Mar 14 '06 #3
Richard Bos wrote:

"ma***********@gmail.com" <ma***********@gmail.com> wrote:
field = strtok(NULL,DELIM2);
while(field != NULL)
{
strcpy(fieldcopy,field);
CreateCopies(copy,fieldcopy,NoCopies);
field = strtok(NULL,DELIM2);
printf("Field in CreateVideo is %s\n",field);
}

Now if I call CreateCopies the strtok doesn't tokenize till the end.
But if i comment the CreateCopies call it does tokenize till the end.


Does CreateCopies perhaps also use strtok()? You can't nest uses of
strtok(), because it keeps only a single, static state.

Well, you can hardly nest invocations of strtok(), but it is *not*
difficult to "roll your own" that *will* support nested invocations.

--
+----------------------------------------------------------------+
| Charles and Francis Richmond richmond at plano dot net |
+----------------------------------------------------------------+
Mar 15 '06 #4
Charles Richmond <ri******@comcast.net> wrote:
Richard Bos wrote:

"ma***********@gmail.com" <ma***********@gmail.com> wrote:
Now if I call CreateCopies the strtok doesn't tokenize till the end.
But if i comment the CreateCopies call it does tokenize till the end.
Does CreateCopies perhaps also use strtok()? You can't nest uses of
strtok(), because it keeps only a single, static state.

Well, you can hardly nest invocations of strtok(), but it is *not*
difficult to "roll your own" that *will* support nested invocations.


That is true. While you're at it, allow it to split "name,address,,city"
into four rather than three fields, as well.
--


That, btw, is not a .sig-sep.

Richard
Mar 15 '06 #5
Charles Richmond wrote:
Richard Bos wrote:
.... snip ...
Does CreateCopies perhaps also use strtok()? You can't nest uses
of strtok(), because it keeps only a single, static state.


Well, you can hardly nest invocations of strtok(), but it is *not*
difficult to "roll your own" that *will* support nested invocations.


Here's a version I posted a while ago. Public Domain.

/* ------- 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/>
Mar 15 '06 #6
On 2006-03-15, Richard Bos <rl*@hoekstra-uitgeverij.nl> wrote:
Charles Richmond <ri******@comcast.net> wrote:
Well, you can hardly nest invocations of strtok(), but it is *not*
difficult to "roll your own" that *will* support nested invocations.


That is true. While you're at it, allow it to split "name,address,,city"
into four rather than three fields, as well.


Should it also split "name address city" to 7 fields as well if ' '
is part of its seperator string?

Obviously, which one is preferable is highly dependant on what you want
to do with it.

--
John Tsiombikas (Nuclear / Mindlapse)
nu*****@siggraph.org
http://nuclear.demoscene.gr/
Mar 16 '06 #7
On 2006-03-16, John Tsiombikas (Nuclear / Mindlapse) <nu*****@siggraph.org> wrote:
On 2006-03-15, Richard Bos <rl*@hoekstra-uitgeverij.nl> wrote:
Charles Richmond <ri******@comcast.net> wrote:
Well, you can hardly nest invocations of strtok(), but it is *not*
difficult to "roll your own" that *will* support nested invocations.


That is true. While you're at it, allow it to split "name,address,,city"
into four rather than three fields, as well.


Should it also split "name address city" to 7 fields as well if ' '
is part of its seperator string?

Obviously, which one is preferable is highly dependant on what you want
to do with it.


So there should be two functions. or maybe a flag to be passed to the
single function.
Mar 16 '06 #8
Jordan Abel wrote:
John Tsiombikas (Nuclear / Mindlapse) <nu*****@siggraph.org> wrote:
On 2006-03-15, Richard Bos <rl*@hoekstra-uitgeverij.nl> wrote:
Charles Richmond <ri******@comcast.net> wrote:

Well, you can hardly nest invocations of strtok(), but it is *not*
difficult to "roll your own" that *will* support nested invocations.

That is true. While you're at it, allow it to split "name,address,,city"
into four rather than three fields, as well.


Should it also split "name address city" to 7 fields as well if ' '
is part of its seperator string?

Obviously, which one is preferable is highly dependant on what you want
to do with it.


So there should be two functions. or maybe a flag to be passed to the
single function.


If you simply say that leading blanks in a token are absorbed, and
that the token char is a separator, the conflicts disappear. See
my toksplit routine elsethread. Which, BTW, is re-entrant.

--
"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/>
Mar 16 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ramprasad A Padmanabhan | last post by:
Hello all I want to read into a string an input from QUERY_STRING how do I ensure that scanf reads more chars into the string that it can hold eg { char* s1; char* data; long n;
9
by: daniel | last post by:
Hi everyone, I'm trying to get this program compiled under Solaris. Unfortunately I have little experience with C. Solaris doesn't use the function strsep() anymore: char *strsep(char...
13
by: ern | last post by:
I'm using strtok( ) to capture lines of input. After I call "splitCommand", I call strtok( ) again to get the next line. Strtok( ) returns NULL (but there is more in the file...). That didn't...
2
by: manochavishal | last post by:
Hi I am writing a Program in which i get input as #C1012,S,A#C1013,S,U I want to get C1012,S,A using strtok and then pass this to function CreateCopies which will further strtok this...
8
by: hu | last post by:
hi, everybody! I'm testing the fuction of strtok(). The environment is WinXP, VC++6.0. Program is simple, but mistake is confusing. First, the below code can get right outcome:"ello world, hello...
8
by: machikelxol | last post by:
I'm having a strange error when I try reading from a file. here is the code: buffstring values = 1, 2, 3, 4 and then it crashes afterwards on the 4th iteration. This works fine until the fourth...
13
by: anant | last post by:
Hi all The below code is reading string and then tokenizin it and reading all the info. But i want to call a csv file and it should then read a string from dt file. So what midification should i...
0
by: shrik | last post by:
I have following error : Total giant files in replay configuration file are : File name : /new_file/prob1.rec Given file /new_file/prob1.rec is successfully verified. Splitting for giant file...
11
by: magicman | last post by:
can anyone point me out to its implementation in C before I roll my own. thx
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.