473,513 Members | 4,753 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

strtok equiavalent in C++ ?


// I am using strtok to break the string
// For example: to extract 5 from the string below:
// TEST 1,P,5,PASS
// Below is my code in C:
ptr =strtok(testbuff," \t\n,()");
ptr =strtok(NULL," \t\n,()");
ptr =strtok(NULL," \t\n,()");
ptr =strtok(NULL," \t\n,()");

// My question: is there a better way to do this in C++,
// assumed testbuff is C++ string type.

// Thanks in advance for any hint,

Oct 24 '05 #1
5 8733
tv****@hotmail.com wrote:
// I am using strtok to break the string
// For example: to extract 5 from the string below:
// TEST 1,P,5,PASS
// Below is my code in C:
ptr =strtok(testbuff," \t\n,()");
ptr =strtok(NULL," \t\n,()");
ptr =strtok(NULL," \t\n,()");
ptr =strtok(NULL," \t\n,()");

// My question: is there a better way to do this in C++,
// assumed testbuff is C++ string type.

// Thanks in advance for any hint,


Check out the Boost Tokenizer library
(http://www.boost.org/libs/tokenizer/).

Cheers! --M

Oct 24 '05 #2
On 2005-10-24, tv****@hotmail.com <tv****@hotmail.com> wrote:

// I am using strtok to break the string
// For example: to extract 5 from the string below:
// TEST 1,P,5,PASS
// Below is my code in C:
ptr =strtok(testbuff," \t\n,()");
ptr =strtok(NULL," \t\n,()");
ptr =strtok(NULL," \t\n,()");
ptr =strtok(NULL," \t\n,()");

// My question: is there a better way to do this in C++,
// assumed testbuff is C++ string type.


The std::string member function find_first_of can do the job of
strtok.

std::string::size_type i = 0;
i = testbuff.find_first_of(" \t\n,()", i);
i = testbuff.find_first_of(" \t\n,()", i);
i = testbuff.find_first_of(" \t\n,()", i);
i = testbuff.find_first_of(" \t\n,()", i);

--
Neil Cerutti
Oct 24 '05 #3
On 2005-10-24, Neil Cerutti <le*******@email.com> wrote:
The std::string member function find_first_of can do the job of
strtok.

std::string::size_type i = 0;
i = testbuff.find_first_of(" \t\n,()", i);
i = testbuff.find_first_of(" \t\n,()", i);
i = testbuff.find_first_of(" \t\n,()", i);
i = testbuff.find_first_of(" \t\n,()", i);


Oops. Sorry. That was utter rubbish.

--
Neil Cerutti
Oct 24 '05 #4

<tv****@hotmail.com> wrote in message news:11*********************@z14g2000cwz.googlegro ups.com...

// I am using strtok to break the string
// For example: to extract 5 from the string below:
// TEST 1,P,5,PASS
// Below is my code in C:
ptr =strtok(testbuff," \t\n,()");
ptr =strtok(NULL," \t\n,()");
ptr =strtok(NULL," \t\n,()");
ptr =strtok(NULL," \t\n,()");

// My question: is there a better way to do this in C++,
// assumed testbuff is C++ string type.

// Thanks in advance for any hint,

Look at sample "Splitting string into vector of vectors" at
* http://groups.google.com/group/sourc...993fb8841382c8
* http://groups.google.com/group/log-f...5be21f949acdcc

--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Oct 25 '05 #5
On 2005-10-25, Alex Vinokur <al****@go.to> wrote:

<tv****@hotmail.com> wrote in message news:11*********************@z14g2000cwz.googlegro ups.com...

// I am using strtok to break the string
// For example: to extract 5 from the string below:
// TEST 1,P,5,PASS
// Below is my code in C:
ptr =strtok(testbuff," \t\n,()");
ptr =strtok(NULL," \t\n,()");
ptr =strtok(NULL," \t\n,()");
ptr =strtok(NULL," \t\n,()");

// My question: is there a better way to do this in C++,
// assumed testbuff is C++ string type.

// Thanks in advance for any hint,

Look at sample "Splitting string into vector of vectors" at
* http://groups.google.com/group/sourc...993fb8841382c8
* http://groups.google.com/group/log-f...5be21f949acdcc


In addition, a chapter of _Accelerated C++_ (Koenig, Moo) is
devoted to developing a standard algorithm style template
function for splitting sequences into subsequences.

--
Neil Cerutti
Oct 25 '05 #6

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

Similar topics

13
4899
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...
20
17179
by: bubunia2000 | last post by:
Hi all, I heard that strtok is not thread safe. So I want to write a sample program which will tokenize string without using strtok. Can I get a sample source code for the same. For exp:...
8
1914
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...
4
2715
by: Michael | last post by:
Hi, I have a proble I don't understand when using strtok(). It seems that if I make a call to strtok(), then make a call to another function that also makes use of strtok(), the original call is...
3
3792
by: nomad5000 | last post by:
Hi everybody! I'm having trouble using strtok to fill a matrix with int nrs. from a file. the code that is not working is the following: #include <iostream> #include <fstream> #include...
29
2550
by: Pietro Cerutti | last post by:
Hello, here I have a strange problem with a real simple strtok example. The program is as follows: ### BEGIN STRTOK ### #include <string.h> #include <stdio.h>
11
904
by: Lothar Behrens | last post by:
Hi, I have selected strtok to be used in my string replacement function. But I lost the last token, if there is one. This string would be replaced select "name", "vorname", "userid",...
11
17151
by: magicman | last post by:
can anyone point me out to its implementation in C before I roll my own. thx
12
2338
by: Pilcrow | last post by:
Here is a quick program, together with its output, that illustrates what I consider to be a deficiency of the standard function strtok from <string.h>: I am using C:\>gcc --version gcc (GCC)...
0
7373
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,...
0
7432
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...
1
7094
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...
0
7519
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...
0
5677
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5079
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...
0
4743
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...
0
3230
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...
0
1585
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 ...

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.