Topic: (Replacement for) strtok in C++?
Author: Marc Koschewski <m.koschewski@freund-co.com>
Date: Thu, 21 Dec 2000 17:31:17 GMT Raw View
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
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: James Dennett <james@evtechnology.com>
Date: Thu, 21 Dec 2000 17:56:10 GMT Raw View
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 library will be compiled under Windows NT/2000, Linux and Solaris ....
> so I cannot use any compiler specific functions or platform dependant stuff.
Be thankful that ISO C++ includes the C library. If you include <string.h>
you'll find ::strtok defined, and if you include <cstring> then it'll be
there as std::strtok. (The latter would be preferable if you don't need
code to compile with a C compiler -- less namespace polution.)
Naturally strtok is still Evil. Firstly, it will cause problems if you
ever multi-thread your application. Secondly, you will need to document
the fact that your library routine foo() calls strtok to ensure that
users of your library don't rely on strtok maintaining state across a
call to foo(). Thirdly, of course, it works by modifying strings in
place, so you need to be careful not to pass genuinely const strings to
it.
>From man strtok(3) on my Linux box:
BUGS
Never use this function. This function modifies its first argument.
The identity of the delimiting character is lost. This function
cannot be used on constant strings.
IMO you'd be better off writing your own function or class to do this.
-- James Dennett <jdennett@acm.org>
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: pedwards@dmapub.dma.org (Phil Edwards)
Date: Thu, 21 Dec 2000 18:57:29 GMT Raw View
James Dennett <james@evtechnology.com> wrote:
+ Marc Koschewski wrote:
+ >
+ > 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?
+ >
[...]
+ >From man strtok(3) on my Linux box:
+ BUGS
+ Never use this function. This function modifies its first argument.
+ The identity of the delimiting character is lost. This function
+ cannot be used on constant strings.
+
+ IMO you'd be better off writing your own function or class to do this.
Something like this?
http://gcc.gnu.org/onlinedocs/libstdc++/21_strings/howto.html#3
There is some discussion there of the Evil That Is Strtok() and two possible
implementations of a replacement called stringtok(), templatized to work
with containers. Even if it doesn't do quite what Mr. Koschewski wants,
it should at least provide a starting point or two.
Luck++;
Phil
--
pedwards at disaster dot jaj dot com | pme at sources dot redhat dot com
devphil at several other less interesting addresses in various dot domains
The gods do not protect fools. Fools are protected by more capable fools.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: Francis Glassborow <francis.glassborow@ntlworld.com>
Date: Thu, 21 Dec 2000 20:28:41 GMT Raw View
In article <91tedl$55895$2@ID-64048.news.dfncis.de>, Marc Koschewski
<m.koschewski@freund-co.com> writes
>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?
Yes, it is called strtok. All the C90 standard library is included in
C++ by reference (to the C Standard)
Francis Glassborow Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]