Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > strtok

Reply
Thread Tools

strtok

 
 
Bill Cunningham
Guest
Posts: n/a
 
      01-01-2012
Why does my man page for strtok and an extension for a thread safe
version of this function say not to use these functions? Are they buggy or
deprecated? What should one use instead?

Bill


 
Reply With Quote
 
 
 
 
Nick Keighley
Guest
Posts: n/a
 
      01-01-2012
On Jan 1, 8:30*pm, "Bill Cunningham" <(E-Mail Removed)> wrote:
> * * Why does my man page for strtok and an extension for a thread safe
> version of this function say not to use these functions? Are they buggy or
> deprecated?


neither,what does the man page say? Some people don't like what
strtok() does. It modifies the string you pass to it and doesn't
handle empty fields in the way you might like.

> What should one use instead?


good question. There's no portable answer. So write your own I
suppose. I've used strtok() its ok it does what it says on the can

 
Reply With Quote
 
 
 
 
Johann Klammer
Guest
Posts: n/a
 
      01-01-2012
Bill Cunningham wrote:
> Why does my man page for strtok and an extension for a thread safe
> version of this function say not to use these functions? Are they buggy or
> deprecated? What should one use instead?
>
> Bill
>
>


To my knowledge, it uses internal state to maintain the current position
which can generate race conditions if used from multiple threads
concurrently.

eglibc has strtok_r() which is a reentrant function.
It allows the user to specify the address of a variable to use for
storing position.

[OT]
This is similar to the errno races observed with threaded code.
Some c libraries thus #define errno to be a function call returning a
pointer, which then is dereferenced to effectively create a thread-local
errno.

 
Reply With Quote
 
osmium
Guest
Posts: n/a
 
      01-01-2012
"Bill Cunningham" wrote:

> Why does my man page for strtok and an extension for a thread safe
> version of this function say not to use these functions? Are they buggy or
> deprecated? What should one use instead?


Bill, I suggest you put any concerns about thread safe operations on the
back burner, next to "How will my program accommodate quantum computers?"

If you would quit trying to learn everything you might possibly learn
*something*.


 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      01-01-2012
Nick Keighley wrote:

> neither,what does the man page say? Some people don't like what
> strtok() does. It modifies the string you pass to it and doesn't
> handle empty fields in the way you might like.
>
>> What should one use instead?

>
> good question. There's no portable answer. So write your own I
> suppose. I've used strtok() its ok it does what it says on the can


Under bugs I have :
"Never use these functions. If you do note
These functions modify first arg
These functions can't be used with const strings.
The identity of the delimiting character is lost.
strtok uses a static buffer while parsing so is not thread safe. If using
threads user strtok_r."

The man pages online don't say this.

Bill


 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      01-01-2012
"Bill Cunningham" <(E-Mail Removed)> writes:

> Why does my man page for strtok and an extension for a thread safe
> version of this function say not to use these functions? Are they buggy or
> deprecated? What should one use instead?


If your question is still about reading your market data (as you posted
on comp.programming) you don't need strtok.

If you are sure that the data will be in the format you previously
described, fscanf can do the job perfectly well. If you want a little
more control, read each line using fgets and use sscanf to "parse" the
data you want.

Example:

#include <stdio.h>

int main(void)
{
double price;
int line_no, day, month, year;
char line[100];
while (fgets(line, sizeof line, stdin) &&
sscanf(line, "%d %lf %2d%2d%2d",
&line_no, &price, &day, &month, &year) == 5)
printf("line number: %d, price=%f on %d/%02d/%02d\n",
line_no, price, day, month, year);
return 0;
}

--
Ben.
 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      01-01-2012
Ben Bacarisse wrote:
> "Bill Cunningham" <(E-Mail Removed)> writes:
>
>> Why does my man page for strtok and an extension for a thread
>> safe version of this function say not to use these functions? Are
>> they buggy or deprecated? What should one use instead?

>
> If your question is still about reading your market data (as you
> posted on comp.programming) you don't need strtok.


Ok

> If you are sure that the data will be in the format you previously
> described, fscanf can do the job perfectly well.


Ok

If you want a little
> more control, read each line using fgets and use sscanf to "parse" the
> data you want.
>
> Example:
>
> #include <stdio.h>
>
> int main(void)
> {
> double price;
> int line_no, day, month, year;
> char line[100];
> while (fgets(line, sizeof line, stdin) &&
> sscanf(line, "%d %lf %2d%2d%2d",
> &line_no, &price, &day, &month, &year) == 5)
> printf("line number: %d, price=%f on %d/%02d/%02d\n",
> line_no, price, day, month, year);
> return 0;
> }


Thanks. That sscanf is alittle hard to read but then again I'm not
familiar with that function.

Bill


 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      01-01-2012
Le 01/01/12 22:10, osmium a écrit :
> "Bill Cunningham" wrote:
>
>> Why does my man page for strtok and an extension for a thread safe
>> version of this function say not to use these functions? Are they buggy or
>> deprecated? What should one use instead?

>
> Bill, I suggest you put any concerns about thread safe operations on the
> back burner, next to "How will my program accommodate quantum computers?"
>
> If you would quit trying to learn everything you might possibly learn
> *something*.
>
>

Good words!

 
Reply With Quote
 
John Tsiombikas
Guest
Posts: n/a
 
      01-02-2012
On 2012-01-01, Bill Cunningham <(E-Mail Removed)> wrote:
> Nick Keighley wrote:
>
>> neither,what does the man page say? Some people don't like what
>> strtok() does. It modifies the string you pass to it and doesn't
>> handle empty fields in the way you might like.
>>
>>> What should one use instead?

>>
>> good question. There's no portable answer. So write your own I
>> suppose. I've used strtok() its ok it does what it says on the can

>
> Under bugs I have :
> "Never use these functions. If you do note


Retard manpage author. Nevermind that, use it if it does what you need,
just be aware of its limitations as others pointed out.

--
John Tsiombikas
http://nuclear.mutantstargoat.com/
 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      01-02-2012
"Bill Cunningham" <(E-Mail Removed)> writes:

> Why does my man page for strtok and an extension for a thread safe
> version of this function say not to use these functions? Are they buggy or
> deprecated? What should one use instead?


strtok() has at least these problems:

* It merges adjacent delimiters. If you use a comma as your
delimiter, then "a,,b,c" will be divided into three tokens,
not four. This is often the wrong thing to do. In fact, it
is only the right thing to do, in my experience, when the
delimiter set contains white space (for dividing a string
into "words") or it is known in advance that there will be
no adjacent delimiters.

* The identity of the delimiter is lost, because it is
changed to a null terminator.

* It modifies the string that it tokenizes. This is bad
because it forces you to make a copy of the string if
you want to use it later. It also means that you can't
tokenize a string literal with it; this is not
necessarily something you'd want to do all the time but
it is surprising.

* It can only be used once at a time. If a sequence of
strtok() calls is ongoing and another one is started,
the state of the first one is lost. This isn't a
problem for small programs but it is easy to lose track
of such things in hierarchies of nested functions in
large programs. In other words, strtok() breaks
encapsulation.

--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x11f6},*p
=b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)break;else default:continue;if(0)case 1utchar(a[i&15]);break;}}}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
strtok() and std::string Alex Vinokur C++ 6 04-14-2005 01:40 PM
Problems with strtok() returning one too many tokens... Adam Balgach C++ 2 11-28-2004 01:12 AM
segfault on strtok Fatih Gey C Programming 40 11-01-2003 07:24 PM
strtok trouble Robert C Programming 17 09-06-2003 10:30 PM
strtok problem jorntk@yahoo.com C Programming 4 08-29-2003 11:26 AM



Advertisments