473,804 Members | 2,164 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help with strtok

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

#C1012,S,A#C101 3,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#C101 3,S,U*/
field = strtok(NULL,DEL IM2);
while(field != NULL)
{
strcpy(fieldcop y,field);
CreateCopies(co py,fieldcopy,No Copies);
field = strtok(NULL,DEL IM2);
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 2247
On 2006-03-14, ma***********@g mail.com <ma***********@ gmail.com> wrote:
Hi
I am writing a Program
in which i get input as

#C1012,S,A#C101 3,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#C101 3,S,U*/
field = strtok(NULL,DEL IM2);
Its not clear if you have called strtok with your field as first
argument before.

while(field != NULL)
{
strcpy(fieldcop y,field);
CreateCopies(co py,fieldcopy,No Copies);
field = strtok(NULL,DEL IM2);
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,DEL IM2);
while(field != NULL)
{
strcpy(fieldcop y,field);
CreateCopies(co py,fieldcopy,No Copies);
field = strtok(NULL,DEL IM2);
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,DEL IM2);
while(field != NULL)
{
strcpy(fieldcop y,field);
CreateCopies(co py,fieldcopy,No Copies);
field = strtok(NULL,DEL IM2);
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******@comca st.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.c om, 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/googlegroupsrep ly/>
Mar 15 '06 #6
On 2006-03-15, Richard Bos <rl*@hoekstra-uitgeverij.nl> wrote:
Charles Richmond <ri******@comca st.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*****@siggrap h.org
http://nuclear.demoscene.gr/
Mar 16 '06 #7
On 2006-03-16, John Tsiombikas (Nuclear / Mindlapse) <nu*****@siggra ph.org> wrote:
On 2006-03-15, Richard Bos <rl*@hoekstra-uitgeverij.nl> wrote:
Charles Richmond <ri******@comca st.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*****@siggra ph.org> wrote:
On 2006-03-15, Richard Bos <rl*@hoekstra-uitgeverij.nl> wrote:
Charles Richmond <ri******@comca st.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.c om, 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/googlegroupsrep ly/>
Mar 16 '06 #9

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

Similar topics

3
4978
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
4010
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 **stringp, const char *delim);
13
4931
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 happen before 'splitCommands' entered the picture. The problem is in splitCommands( ) somehow modifying the pointer, but I HAVE to call that function. Is there a way to make a copy of it or something ? /* HERE IS MY CODE */ char *...
2
2092
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 (C1012,S,A) and store the required
8
1932
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 dreams." #include <stdafx.h> #include <string.h> #include <stdio.h> int main()
8
1799
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 iteration. breaks on this line: buffString = new char; The contents of the text file it is reading: 1,1,1,1,1,1,1,1,1,5,6,7,8,9 2,2,2,2,2,2,2,2,2 3,3,3,3,3,3,3,3,3
13
2177
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 do in the below code. Its main code, all the functions are defined well. So just wanna know how to include some extra code to open the file and read each line. Thanks a lot typedef struct {
0
6551
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 /new_file/prob1.rec started. Please wait.... In while loop of request searching *** glibc detected *** ./a.out: free(): invalid next size (normal): 0x099da890 *** ======= Backtrace: ========= /lib/libc.so.6
11
17180
by: magicman | last post by:
can anyone point me out to its implementation in C before I roll my own. thx
0
9714
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10600
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10350
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10351
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7638
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5534
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4311
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3834
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3002
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.