使用strtok [英] using strtok

查看:68
本文介绍了使用strtok的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我突然想到了使用strtok的一个有趣的限制。


我有两个字符串,我想要strtok操作。

但是从strtok开始只有一个剩余字符串的内存我必须

在第二个开始之前完成一组操作。这个

在我的程序环境中不方便!


到目前为止,我能看到的唯一解决方案是为strtok写一个替代品

用于其中一个字符串。任何人都可以提供替代方案吗?


-

_ _________________________________________

/ \ ._._ | _ _ _ / ''Orpheus互联网服务

\_ / | | _)| |(/ _ | _ | _ /''互联网为每个人''

_______ | ___________./ http://www.orpheusinternet.co.uk

I cam across an interesting limitation to the use of strtok.

I have two strings on which I want strtok to operate.
However since strtok has only one memory of the residual string I must
complete one set of operations before starting on the second. This
is inconvenient in the context of my program!

So far the only solution I can see is to write a replacement for strtok
to use on one of the strings. Can anyone offer an alternative?

--
_ _________________________________________
/ \._._ |_ _ _ /'' Orpheus Internet Services
\_/| |_)| |(/_|_|_/ ''Internet for Everyone''
_______ | ___________./ http://www.orpheusinternet.co.uk

推荐答案

3月10日,12:35下午,John FO Evans先生< m ... @ orp​​heusmail.co.ukwrote:
On Mar 10, 12:35 pm, Mr John FO Evans <m...@orpheusmail.co.ukwrote:

我突然想到使用strtok的一个有趣的限制。


我有两个字符串,我想要strtok操作。

但是由于strtok只有一个剩余字符串的内存我必须

在第二个开始之前完成一组操作。这个

在我的程序环境中不方便!


到目前为止,我能看到的唯一解决方案是写一个替换strtok

用于其中一个字符串。任何人都可以提供替代方案吗?
I cam across an interesting limitation to the use of strtok.

I have two strings on which I want strtok to operate.
However since strtok has only one memory of the residual string I must
complete one set of operations before starting on the second. This
is inconvenient in the context of my program!

So far the only solution I can see is to write a replacement for strtok
to use on one of the strings. Can anyone offer an alternative?



不是真的,除非你'我愿意使用< OT> POSIX / SUS''ss trtok_r()< /

OT并跳过可移植性。否则,请查看CBFalconer的

替换toksplit(),在
http://groups.google.com/group/comp....58085dd57c3a5b

-

WYCIWYG - 你得到的是什么

Not really, unless you''d be willing to use <OT>POSIX/SUS''s strtok_r()</
OTand skip on the portability. Otherwise, look at CBFalconer''s
replacement toksplit(), discussed at
http://groups.google.com/group/comp....58085dd57c3a5b.
--
WYCIWYG - what you C is what you get




John FO Evans先生写道:

Mr John FO Evans wrote:

我对strtok的使用有一个有趣的限制。


我有两个字符串我想要strtok到操作。

但是由于strtok只有一个剩余字符串的内存,我必须在第二个开始之前完成一组操作。这个

在我的程序环境中不方便!


到目前为止,我能看到的唯一解决方案是为strtok写一个替代品

用于其中一个字符串。有人可以提供另类选择吗?
I cam across an interesting limitation to the use of strtok.

I have two strings on which I want strtok to operate.
However since strtok has only one memory of the residual string I must
complete one set of operations before starting on the second. This
is inconvenient in the context of my program!

So far the only solution I can see is to write a replacement for strtok
to use on one of the strings. Can anyone offer an alternative?



POSIX指定了一个strtok_r,旨在解决strtok的

重入问题。但是,如果你想要完全的便携性,那么你需要推出自己的版本。这并不难,可以用完全标准的C来完成.BBFalconer定期向这个组发布他的

toksplit功能。使用Google Group的搜索工具

找到来源。

POSIX specifies a strtok_r that was designed to work around the
reentrancy issue of strtok. If you want full portability however,
you''ll have to roll your own version. It''s not difficult and can be
done in completely standard C. CBFalconer periodically publishes his
toksplit function to this group. Use Google Group''s search facility to
locate the source.


Mr John FO Evans写道:
Mr John FO Evans wrote:

>

我在使用strtok时遇到了一个有趣的限制。


我有两个字符串我希望strtok能够运行。

但是由于strtok只有一个剩余字符串的内存,所以
必须在第二个开始之前完成一组操作。 />
这在我的程序中是不方便的!


到目前为止,我能看到的唯一解决方案是为

写一个替代品strtok用于其中一个字符串。任何人都可以提供

替代品吗?
>
I cam across an interesting limitation to the use of strtok.

I have two strings on which I want strtok to operate.
However since strtok has only one memory of the residual string I
must complete one set of operations before starting on the second.
This is inconvenient in the context of my program!

So far the only solution I can see is to write a replacement for
strtok to use on one of the strings. Can anyone offer an
alternative?



试试这个:


/ * ------- file toksplit.c ------ ---- * /

#include" toksplit.h"

/ *复制输入字符串中的下一个标记,在

跳过前导空格(或其他空格?)。

令牌首先出现在tokchar,

或源字符串末尾。


来电者必须在令牌中提供足够的空间以便

收到任何令牌,否则令牌将被截断。


返回:指针越过终止的tokchar。


如果用src指向字符串末尾调用

,这将很高兴地返回无限的空标记。代币

将永远不会包含tokchar的副本。


更好的名称将是strtkn,除了保留

表示系统命名空间。更改为您的风险。


由C.B. Falconer发布到公共领域。

发布2006-02-20。归因于赞赏。

修订2006-06-13

* /


const char * toksplit(const char * src,/ *令牌来源* /

char tokchar,/ * token delimiting char * /

char * token,/ *解析令牌的接收者* /

size_t lgh)/ *长度令牌可以收到* /

/ *不包括最终''\ 0''* /

{

if(src){

while('''== * src)src ++;


while(* src&&(tokchar) != * src)){

if(lgh){

* token ++ = * src;

--lgh;

}

src ++;

}

if(* src&&(tokchar == * src))src ++;

}

* token =''\ 0'';

返回src;

} / * toksplit * /


#ifdef测试

#include< stdio.h>


#define ABRsize 6 / *可接受的令牌缩写长度* /


/ * ---------------- * /

static void showtoken(int i,char * tok)

{

putchar(i +''1''); putchar('':'');

puts(tok);

} / * showtoken * /


/ * ---------------- * /


int main(无效)

{

char teststring [] ="这是一个测试,,,缩写,更多;


const char * t,* s = teststring;

int i;

char token [ABRsize + 1];


puts(teststring);

t = s;

for(i = 0; i< 4; i ++){

t = toksplit(t,'','',token,ABRsize);

showtoken(i,token);

}


put(\ n如何在截断时检测''不再有令牌'') ;

t = s; i = 0;

while(* t){

t = toksplit(t,'','',token,3);

showtoken(i,token);

i ++;

}


puts(" \ nUsing blanks as token delimiters" );

t = s; i = 0;

而(* t){

t = toksplit(t,'''',令牌,ABRsize);

showtoken (i,令牌);

i ++;

}

返回0;

} / * main * /


#endif

/ * -------结束文件toksplit.c ---------- * /


/ * -------文件toksplit.h ---------- * /

#ifndef H_toksplit_h

#define H_toksplit_h


#ifdef __cplusplus

extern" C" {

#endif


#include< stddef.h>


/ *复制下一个令牌从输入字符串开始,在
跳过前导空格(或其他空格?)之后。

令牌首先出现在tokchar,

或源字符串末尾。


来电者必须在令牌中提供足够的空间以便

收到任何令牌,否则令牌将被截断。


返回:指针越过终止的tokchar。


如果用src指向字符串末尾调用

,这将很高兴地返回无限的空标记。代币

将永远不会包含tokchar的副本。


由CB Falconer发布到公共领域。

发布2006-02 -20。归因于赞赏。

* /


const char * toksplit(const char * src,/ *代币来源* /

char tokchar,/ * token delimiting char * /

char * token,/ *解析令牌的接收者* /

size_t lgh); / *长度令牌可以收到* /

/ *不包括最终''\ 0''* /


#ifdef __cplusplus

}

#endif

#endif

/ * -------结束文件toksplit.h ---- ------ * /


-

< http://www.cs.auckland.ac.nz/~pgut001/pubs /vista_cost.txt>

< http://www.securityfocus.com/columnists/423>


"每次都是正确的男人不太可能做得太多。

- Francis Crick,共同发现DNA

没有什么比行动中的愚蠢更令人惊讶了。

- Thomas Matthews


-

通过 http://www.teranews.com

Try this:

/* ------- file toksplit.c ----------*/
#include "toksplit.h"

/* copy over the next token from an input string, after
skipping leading blanks (or other whitespace?). The
token is terminated by the first appearance of tokchar,
or by the end of the source string.

The caller must supply sufficient space in token to
receive any token, Otherwise tokens will be truncated.

Returns: a pointer past the terminating tokchar.

This will happily return an infinity of empty tokens if
called with src pointing to the end of a string. Tokens
will never include a copy of tokchar.

A better name would be "strtkn", except that is reserved
for the system namespace. Change to that at your risk.

released to Public Domain, by C.B. Falconer.
Published 2006-02-20. Attribution appreciated.
Revised 2006-06-13
*/

const char *toksplit(const char *src, /* Source of tokens */
char tokchar, /* token delimiting char */
char *token, /* receiver of parsed token */
size_t lgh) /* length token can receive */
/* not including final ''\0'' */
{
if (src) {
while ('' '' == *src) src++;

while (*src && (tokchar != *src)) {
if (lgh) {
*token++ = *src;
--lgh;
}
src++;
}
if (*src && (tokchar == *src)) src++;
}
*token = ''\0'';
return src;
} /* toksplit */

#ifdef TESTING
#include <stdio.h>

#define ABRsize 6 /* length of acceptable token abbreviations */

/* ---------------- */

static void showtoken(int i, char *tok)
{
putchar(i + ''1''); putchar('':'');
puts(tok);
} /* showtoken */

/* ---------------- */

int main(void)
{
char teststring[] = "This is a test, ,, abbrev, more";

const char *t, *s = teststring;
int i;
char token[ABRsize + 1];

puts(teststring);
t = s;
for (i = 0; i < 4; i++) {
t = toksplit(t, '','', token, ABRsize);
showtoken(i, token);
}

puts("\nHow to detect ''no more tokens'' while truncating");
t = s; i = 0;
while (*t) {
t = toksplit(t, '','', token, 3);
showtoken(i, token);
i++;
}

puts("\nUsing blanks as token delimiters");
t = s; i = 0;
while (*t) {
t = toksplit(t, '' '', token, ABRsize);
showtoken(i, token);
i++;
}
return 0;
} /* main */

#endif
/* ------- end file toksplit.c ----------*/

/* ------- file toksplit.h ----------*/
#ifndef H_toksplit_h
# define H_toksplit_h

# ifdef __cplusplus
extern "C" {
# endif

#include <stddef.h>

/* copy over the next token from an input string, after
skipping leading blanks (or other whitespace?). The
token is terminated by the first appearance of tokchar,
or by the end of the source string.

The caller must supply sufficient space in token to
receive any token, Otherwise tokens will be truncated.

Returns: a pointer past the terminating tokchar.

This will happily return an infinity of empty tokens if
called with src pointing to the end of a string. Tokens
will never include a copy of tokchar.

released to Public Domain, by C.B. Falconer.
Published 2006-02-20. Attribution appreciated.
*/

const char *toksplit(const char *src, /* Source of tokens */
char tokchar, /* token delimiting char */
char *token, /* receiver of parsed token */
size_t lgh); /* length token can receive */
/* not including final ''\0'' */

# ifdef __cplusplus
}
# endif
#endif
/* ------- end file toksplit.h ----------*/

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews

--
Posted via a free Usenet account from http://www.teranews.com


这篇关于使用strtok的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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