By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,948 Members | 1,196 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,948 IT Pros & Developers. It's quick & easy.

strtok() functionality in C++

P: n/a
Is there a "C++ way" of doing what strtok() does, or should I just
convert string objects into char arrays and use strtok()? I couldn't
find a C++ way that is as straight forward as using strtok().

Would the following be considered "good" C++ style?

s = strtok(line.c_str(), " \t\n");
while (s) {
/* use s here */
s = strtok(NULL, " \t\n");
}
Nov 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a

"Tydr Schnubbis" <fa**@address.dude> wrote in message
news:Mo*******************@juliett.dax.net...
Is there a "C++ way" of doing what strtok() does,
Yes. strtok() (It's as much a part of C++ as it is of C).
or should I just convert string objects into char arrays and use strtok()?
I couldn't find a C++ way that is as straight forward as using strtok().

Would the following be considered "good" C++ style?

s = strtok(line.c_str(), " \t\n");
while (s) {
/* use s here */
s = strtok(NULL, " \t\n");
}


That depends entirely upon your needs.

You can indeed tokenize a std::string object in various
ways. One tool to that end is std::string::find_first_of()
(which will locate a delimiter from a specified set,
but doesn't replace it with a NULL as does strtok())

-Mike
Nov 22 '05 #2

P: n/a
Tydr Schnubbis wrote:
Is there a "C++ way" of doing what strtok() does, or should I just
convert string objects into char arrays and use strtok()? I couldn't
find a C++ way that is as straight forward as using strtok().

Would the following be considered "good" C++ style?

s = strtok(line.c_str(), " \t\n");
while (s) {
/* use s here */
s = strtok(NULL, " \t\n");
}


No it should not compile. strtok modifies it's argument and c_str()
returns a const char*.

There is no built in C++ string tokeniser. It's not too hard to write
your own especially if you keep it simple, or you could get a third
party one, for instance

http://www.boost.org/libs/tokenizer/index.html

john
Nov 22 '05 #3

P: n/a

Tydr Schnubbis wrote:
Is there a "C++ way" of doing what strtok() does, or should I just
convert string objects into char arrays and use strtok()? I couldn't
find a C++ way that is as straight forward as using strtok().

Would the following be considered "good" C++ style?

s = strtok(line.c_str(), " \t\n");
while (s) {
/* use s here */
s = strtok(NULL, " \t\n");
}


If whitespace is all you need as separators, then this should work for
you:

std::istringstream iss( line );
std::string token;
while( iss >> token )
{
// use 'token' here
}

Nov 22 '05 #4

P: n/a
in*****@gmail.com wrote:
Tydr Schnubbis wrote:
Is there a "C++ way" of doing what strtok() does, or should I just
convert string objects into char arrays and use strtok()? I couldn't
find a C++ way that is as straight forward as using strtok().

Would the following be considered "good" C++ style?

s = strtok(line.c_str(), " \t\n");
while (s) {
/* use s here */
s = strtok(NULL, " \t\n");
}


If whitespace is all you need as separators, then this should work for
you:

std::istringstream iss( line );
std::string token;
while( iss >> token )
{
// use 'token' here
}

Thanks, that was just what I was looking for. Simpler and cleaner than
using strtok, as long as you don't require the extra flexibility.
Nov 22 '05 #5

P: n/a
Tydr Schnubbis <fa**@address.dude> writes:
in*****@gmail.com wrote:
Tydr Schnubbis wrote:
Is there a "C++ way" of doing what strtok() does, or should I just
convert string objects into char arrays and use strtok()? I couldn't
find a C++ way that is as straight forward as using strtok().

Would the following be considered "good" C++ style?

s = strtok(line.c_str(), " \t\n");
while (s) {
/* use s here */
s = strtok(NULL, " \t\n");
}

If whitespace is all you need as separators, then this should work
for
you:
std::istringstream iss( line );
std::string token;
while( iss >> token )
{
// use 'token' here
}

Thanks, that was just what I was looking for. Simpler and cleaner
than using strtok, as long as you don't require the extra flexibility.


And if you do need the extra flexibility: (untested)

string next_token(const string& line, const string& delim, string::size_type& first)
{
string::size_type last = line.find_first_of(delim, first);
string next(line.substr(first, last));
first = last;
return next;
}

/Niklas Norrthon
Nov 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.