Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

(Replacement for) strtok in C++?

310 views
Skip to first unread message

Marc Koschewski

unread,
Dec 21, 2000, 6:48:29 PM12/21/00
to
Hi all!

For a small library I need a function like C's strtok. I doubt it's correct
to use it as it's ANSI but not C++. Is there any equivalent function in the
Standard Library that serves the same purpose? If not, does anyone have a
smart idea or a snippet handy?

The library will be compiled under Windows NT/2000, Linux and Solaris ....
so I cannot use any compiler specific functions or platform dependant stuff.

Thx!
Marc

[ Send an empty e-mail to c++-...@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]

Michiel Salters

unread,
Dec 22, 2000, 5:51:32 AM12/22/00
to
Marc Koschewski wrote:

> Hi all!

> For a small library I need a function like C's strtok. I doubt it's correct
> to use it as it's ANSI but not C++. Is there any equivalent function in the
> Standard Library that serves the same purpose? If not, does anyone have a
> smart idea or a snippet handy?

Question: what do you mean by ANSI but not C++? ANSI is a standards
organization. Do you mean ANSI C? If something is ANSI C it's also ANSI C++
(Almost always). An ANSI C program wll therefore be an ANSI C++ program,
even if it contains strtok(). You might want to use <cstring> to get
std::strtok() to show you're really programming C++.

> Thx!
> Marc


HTH,
--
Michiel Salters
Michiel...@cmg.nl
sal...@lucent.com

Alexander Belopolsky

unread,
Dec 22, 2000, 3:52:41 PM12/22/00
to

> Marc Koschewski wrote:
>
> > Hi all!
>
> > For a small library I need a function like C's strtok.

Check libstdc++ FAQ. It used to reside somewhere on gnu.org,
but now the only working copy I found is at

http://docs.et.fh-osnabrueck.de/doku/libstdcpp/21_strings/howto.html

--
Alexander Belopolsky

Brainbench C/C++ Programming “Most Valuable Professional”
www.brainbench.com


Sent via Deja.com
http://www.deja.com/

Brian McNamara!

unread,
Dec 22, 2000, 11:27:27 PM12/22/00
to
m.kosc...@freund-co.com once said:
>For a small library I need a function like C's strtok. I doubt it's correct
>to use it as it's ANSI but not C++. Is there any equivalent function in the
>Standard Library that serves the same purpose? If not, does anyone have a
>smart idea or a snippet handy?

A friend of mine once asked for something that worked like Java's
StringTokenizer class, and I hacked this up:

#include <iostream>
#include <string>
// using namespace std; // I know, I know, old g++

class tokenizer {
string s, delim;
string::size_type pos;
public:
tokenizer( string xs, string xdelim ) : s(xs), delim(xdelim) {
pos = s.find_first_not_of(delim);
}
bool has_more_tokens() { return pos!=string::npos; }
string next_token() {
string::size_type end_pos = s.find_first_of(delim,pos);
string token = s.substr( pos, end_pos-pos );
pos = s.find_first_not_of(delim,end_pos);
return token;
}
};

int main() {
string test = " Tokenize me, \n baby! ";
string delim = "\n\t ";
tokenizer t( test, delim );
while( t.has_more_tokens() )
cout << t.next_token() << endl;
return 0;
}

It may have a bug or two, but it should be enough to work with to create
something that suits your needs, I'd guess.

--
Brian M. McNamara lor...@acm.org : I am a parsing fool!
** Reduce - Reuse - Recycle ** : (Where's my medication? ;) )

Jim Cobban

unread,
Dec 22, 2000, 11:28:40 PM12/22/00
to

"Marc Koschewski" <m.kosc...@freund-co.com> wrote in message
news:91tefb$55895$3...@ID-64048.news.dfncis.de...

> For a small library I need a function like C's strtok. I doubt it's
correct
> to use it as it's ANSI but not C++. Is there any equivalent function in
the
> Standard Library that serves the same purpose? If not, does anyone have a
> smart idea or a snippet handy?
>
> The library will be compiled under Windows NT/2000, Linux and Solaris ....
> so I cannot use any compiler specific functions or platform dependant
stuff.

std::strtok is defined in <cstream> even though I note that Stroustrup,
among other popular references, does not mention this fact.

However strtok has implementation problems, including that it is not
reentrant because it stores progress information in a static table. It also
works by modifying the input string, which is undesirable in many
applications. If your compiler supports it strtok_r is safer (defined by
Common UNIX and therefore certainly present in Linux and Solaris, but
probably not in Win NT). However strtok_r is not in the C++ standard. The
other problem, of course, is that strtok does not use std::string.

Pete Becker

unread,
Dec 23, 2000, 5:42:28 PM12/23/00
to
Jim Cobban wrote:
>
> std::strtok is defined in <cstream> even though I note that Stroustrup,
> among other popular references, does not mention this fact.
>

With good reason: there's no such header. <g> I assume that's a typo for
<cstring>.

--
Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Contributing Editor, C/C++ Users Journal (http://www.cuj.com)

James Moe

unread,
Dec 24, 2000, 8:47:17 AM12/24/00
to
Marc Koschewski wrote:
>
> Hi all!
>
> For a small library I need a function like C's strtok. I doubt it's correct
> to use it as it's ANSI but not C++. Is there any equivalent function in the
> Standard Library that serves the same purpose? If not, does anyone have a
> smart idea or a snippet handy?
>
The standard library <string> has two functions that will help:
find_first_of();
find_not_first_of();

(Disclaimer: Those are from memory. Check your string library to verify
the names.)


--
sma at sohnen-moe dot com

0 new messages