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");
} 5 8762
"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
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
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
} 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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Steven |
last post by:
I need a way to use strtok or some other function to parse a string but keep
the original separator.
My string that is delimited by commas and spaces and I need to format the
string for output...
|
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...
|
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:...
|
by: Peter |
last post by:
hi all, the strtok() cannot phrase the token within another token, am i
correct?
For example, i want to get the second word of every row of a file, how
to use strok to complete this?
thanks...
|
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...
|
by: ryampolsky |
last post by:
I'm using strtok to break apart a colon-delimited string. It basically
works, but it looks like strtok skips over empty sections. In other
words, if the string has 2 colons in a row, it doesn't...
|
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>
|
by: mattmao |
last post by:
Here is my code, just to test the strtok functionality:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct sampleClass
{
int num;
char *text;
|
by: magicman |
last post by:
can anyone point me out to its implementation in C before I roll my
own.
thx
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
| |