如何在不使用strtok的情况下对字符串进行标记 [英] How to tokenize string without using strtok

查看:95
本文介绍了如何在不使用strtok的情况下对字符串进行标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我听说strtok不是线程安全的。所以我想写一个

示例程序,它会在不使用strtok的情况下对字符串进行标记。


我可以得到相同的示例源代码。


对于exp:

0.0.0.0 --->我想使用分隔符将字符串标记为点。


问候

Sujeet

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:
0.0.0.0--->I want to tokenize the string using delimiter as as dot.

Regards
Sujeet

推荐答案

文章< 11 ******** **************@f14g2000cwb.googlegroups .com> ;,

< bu ********* @ yahoo.co.in>写道:
In article <11**********************@f14g2000cwb.googlegroups .com>,
<bu*********@yahoo.co.in> wrote:
我听说strtok不是线程安全的。所以我想编写一个
示例程序,它将在不使用strtok的情况下对字符串进行标记。


正确,strtok不是线程安全的,但C本身不是线程安全的

所以问题不在C标准的范围内。 />
我可以获得相同的示例源代码。


如果你的系统提供了线程设施,那么它也可能会提供strtok_r(),这是一个线程安全工作的常用名称。 />
strtok()。

对于exp:
0.0.0.0 --->我想使用分隔符将字符串标记为点。
I heard that strtok is not thread safe. So I want to write a
sample program which will tokenize string without using strtok.
Correct, strtok is not thread safe, but C itself is not thread safe
so the matter is not within the perview of the C standard.
Can I get a sample source code for the same.
If your system provides thread facilities, it may well also
provide strtok_r(), a common name for a thread-safe work-alike to
strtok() .
For exp:
0.0.0.0--->I want to tokenize the string using delimiter as as dot.




使用strcspn(),然后使用一个计数大小的内存复制例程。


但是之后你必须弄明白你想做什么如果

是多个分界符 - 例如,你想怎么解析


0.0..255.255


这是0.0.0.255,最后还有额外的.255,或者它是多少
0.0.255.255,超额分隔符被跳过,或者它是

0.0然后是一个指标,你已经到了第一个分组的结尾,

然后是第二个分组255.255,或者是

它是解析为指示范围0.0到255.255?

-

没有人有权利o通过要求经验证据来摧毁另一个人的信念。 - Ann Landers



Use strcspn(), then one of the counted-size memory copy routines.

But after that you''ll have to figure out what you want to do if there
are multiple delimeters -- for example, how would you want to parse

0.0..255.255

Is that 0.0.0.255 with an extra .255 at the end, or is it
0.0.255.255 with the extra delimeter skipped, or is it
0.0 then an indicator that you''ve reached the end of the first grouping,
which is then followed by a second grouping 255.255, or is
it to be parsed as indicating the range 0.0 through 255.255 ?
--
"No one has the right to destroy another person''s belief by
demanding empirical evidence." -- Ann Landers




< bu ********* @ yahoo.co.in>在消息中写道

news:11 ********************** @ f14g2000cwb.googlegr oups.com ...

<bu*********@yahoo.co.in> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
大家好,
我听说strtok不是线程安全的。所以我想写一个
示例程序,它会在不使用strtok的情况下对字符串进行标记。

我是否可以获得相同的示例源代码。

对于exp:
0.0.0.0 --->我想使用分隔符将字符串标记为点。

关注
Sujeet
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:
0.0.0.0--->I want to tokenize the string using delimiter as as dot.

Regards
Sujeet




一种方法是编写一个类似于strtok()的函数,但是跟踪放置NULL的最后一个地方的

。一般来说,这可能意味着两个额外的参数:数组中的索引,以及那里的字符。

类似的东西:


char * mystrtok(char * string,char * delimiters,int * index,char * c)

{

/ *写下你的作业的真实代码这里找到n并设置索引

和c * /

返回字符串+ n; / *代币* /

}

-

Fred L. Kleinschmidt

波音助理技术研究员

技术架构师,软件重用项目



One way is to write a function similar to strtok() but keep track of the
last place that a NULL was placed. In general this will probably mean two
extra parameters: the index in the array, and what the character there was.
Something like:

char *mystrtok( char *string, char *delimiters, int *index, char *c )
{
/* write the real code for your homework here to find n and to set index
and c */
return string+n; /* the token */
}
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project


2006-02-16,Walter Roberson< ro ****** @ ibd.nrc -cnrc.gc.ca>写道:
On 2006-02-16, Walter Roberson <ro******@ibd.nrc-cnrc.gc.ca> wrote:
在文章< 11 ********************** @ f14g2000cwb.googlegroups .com>,
< ; BU ********* @ yahoo.co.in>写道:
In article <11**********************@f14g2000cwb.googlegroups .com>,
<bu*********@yahoo.co.in> wrote:
我听说strtok不是线程安全的。所以我想编写一个
示例程序,它将在不使用strtok的情况下对字符串进行标记。
I heard that strtok is not thread safe. So I want to write a
sample program which will tokenize string without using strtok.



正确,strtok不是线程安全的,但C本身不是线程安全的所以问题不在C标准的范围内。



Correct, strtok is not thread safe, but C itself is not thread safe
so the matter is not within the perview of the C standard.




strtok也因为同样的原因而不安全 - 你不能开始
字符串上的标记化,而你在另一个字符串的中间,然后

然后回到第一个。



strtok is also otherwise unsafe for the same reason - you can''t start a
tokenization on a string while you''re in the middle of another one and
then come back to the first.


这篇关于如何在不使用strtok的情况下对字符串进行标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆