473,804 Members | 2,139 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
CreateCopies
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);

}
1.Now if I call CreateCopies the strtok doesn't tokenize till the end.
I get to call CreateCopies
only once.
2.But if i comment the CreateCopies call, it does tokenize till the end
and prints the rrquired vales.
In the first case the second time i call strtok 'field' gets a value of
NULL instead it should get the second token.

Why this behaviour???

Mar 14 '06 #1
2 2092
On Tuesday 14 March 2006 09:18, ma***********@g mail.com opined (in
<11************ **********@p10g 2000cwp.googleg roups.com>):
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
CreateCopies 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);
}
1.Now if I call CreateCopies the strtok doesn't tokenize till the end.
I get to call CreateCopies only once.
2.But if i comment the CreateCopies call, it does tokenize till the
end and prints the rrquired vales.
In the first case the second time i call strtok 'field' gets a value
of NULL instead it should get the second token.

Why this behaviour???


You don't show or tell what `CreateCopies` does, but I'll bet you it
uses `strtok` as well. If it does, therein lies your problem. You're
"resetting" `strtok`. You have to finish tokenising the original string
with `strtok(NULL,.. .)` calls before calling `strtok` again with the
new string to parse.

--
BR, Vladimir

The average, healthy, well-adjusted adult gets up at seven-thirty in
the morning feeling just terrible.
-- Jean Kerr

Mar 14 '06 #2
>You don't show or tell what `CreateCopies` does, but I'll bet you it
uses `strtok` as well. If it does, therein lies your problem. You're
"resetting" `strtok`. You have to finish tokenising the original string
with `strtok(NULL,.. .)` calls before calling `strtok` again with the
new string to parse.


Thanx that did solved my Problem.

I forgot to comment one strtok in CreateCopies.

Thanks again.

Mar 14 '06 #3

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 *...
8
2247
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 CreateVideo which will further strtok this (C1012,S,A) and store the required values.
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
9594
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10096
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
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
6866
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
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...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
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
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.