缺乏strtok [英] Deficiency of strtok

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

问题描述



这是一个快速程序,连同它的输出,说明

我认为是标准函数strtok的缺陷来自

< string.h>:


Here is a quick program, together with its output, that illustrates what
I consider to be a deficiency of the standard function strtok from
<string.h>:


> #include< stdio.h>
#include< string.h>

char line1 [] =" a,bbb,cc",

line2 [] =",pp,qqq",

line3 [ ] =" r ,, s",

line4 [] =" r ,, s",

* token1,* token2,* token3;

int main()
$

puts(&#Line#Input Tokenized\\\
"

" ----- - ------- -------------");


printf(" 1:%8s",line1);

token1 = strtok(line1,",");

token2 = strtok(NULL,",");

token3 = strtok(NULL,",");

printf("%s-%s-%s \ n",token1,token2,token3);


p rintf(" 2:%8s",line2);

token1 = strtok(line2,",");

token2 = strtok(NULL," ,");

token3 = strtok(NULL,",");

printf(" %s-%s-%s \ n",token1,token2,token3);


printf(" 3:%8s",line3);

token1 = strtok(line3,",");

token2 = strtok(NULL,",");

token3 = strtok(NULL ,");

printf("%s-%s-%s \ n",token1,token2,token3);


printf(" 4:%8s",line4);

token1 = strtok(line4,",");

token2 = strtok(NULL, ",");

token3 = strtok(NULL,",");

printf("%s-%s-%s \ n",token1,token2,token3);

return(0);
}

/ *输出:

线#输入标记
----- -------- -------------
1:a,bbb,cc a-bbb-cc
2:,pp,qqq pp-qqq-(null)
3:r ,, s rs-(null)
4:r ,, s r- -s

* /
>#include <stdio.h>
#include <string.h>

char line1[] = "a,bbb,cc",
line2[] = ",pp,qqq",
line3[] = "r,,s",
line4[] = "r, ,s",
*token1, *token2, *token3;

int main()
{
puts("Line# Input Tokenized\n"
"----- -------- -------------");

printf("1: %8s", line1);
token1 = strtok(line1,",");
token2 = strtok(NULL,",");
token3 = strtok(NULL,",");
printf(" %s-%s-%s\n",token1,token2,token3);

printf("2: %8s", line2);
token1 = strtok(line2,",");
token2 = strtok(NULL,",");
token3 = strtok(NULL,",");
printf(" %s-%s-%s\n",token1,token2,token3);

printf("3: %8s", line3);
token1 = strtok(line3,",");
token2 = strtok(NULL,",");
token3 = strtok(NULL,",");
printf(" %s-%s-%s\n",token1,token2,token3);

printf("4: %8s", line4);
token1 = strtok(line4,",");
token2 = strtok(NULL,",");
token3 = strtok(NULL,",");
printf(" %s-%s-%s\n",token1,token2,token3);
return(0);
}
/* OUTPUT:

Line# Input Tokenized
----- -------- -------------
1: a,bbb,cc a-bbb-cc
2: ,pp,qqq pp-qqq-(null)
3: r,,s r-s-(null)
4: r, ,s r- -s

*/



我正在使用

C:\> gcc --version

gcc (GCC) 3.4.5(mingw special)


我希望有一个默认值,当两个分隔符

(逗号,这里)相邻时返回其他。


有没有一种方法可以在不编写我自己的功能的情况下获得此效果?

还有其他一些解决方法吗?


我是C新手,在perl经验丰富,这将是鸭汤。


I am using
C:\>gcc --version
gcc (GCC) 3.4.5 (mingw special)

I would like there to be a default, to be returned when two delimiters
(the commas, here) abut each other.

Is there a way of getting this effect without writing my own function?
Is there some other workaround?

I am new to C, being experienced in perl, where this would be duck soup.


推荐答案

2008年8月7日星期四17:14:29 -0700,

Pilcrow< pi ***** @ pp.infowrote:
On Thu, 07 Aug 2008 17:14:29 -0700,
Pilcrow <pi*****@pp.infowrote:

>

这是一个快速的程序,连同它的输出,说明了什么

我认为是标准函数strtok的缺陷来自
>
Here is a quick program, together with its output, that illustrates what
I consider to be a deficiency of the standard function strtok from



[snip]

[snip]


我希望有一个默认值,当两个分隔符

(这里的逗号)彼此相邻。
I would like there to be a default, to be returned when two delimiters
(the commas, here) abut each other.



我想我从上面了解到你有一个问题

strtok()看到一系列分隔符作为单个

分隔符?

I think I understood from the above that you have a problem with
strtok() seeing a sequence of separator characters as a single
separator?


有没有办法在不编写自己的函数的情况下获得此效果?

是否有一些其他解决方法?
Is there a way of getting this effect without writing my own function?
Is there some other workaround?



如果你不想使用C99功能,你可以考虑使用strcspn()创建

,这不会''如果你只有一个分隔符,你可以使用strchr()。

Martien

-

|

Martien Verbruggen |我曾经有过Heisenbergmobile。每次

|我看了车速表,我迷路了。

|

If you don''t want to use C99 features, you could consider creating
something with strcspn(), which wouldn''t be too hard, or you
could use strchr() if you only have the one separator character.

Martien
--
|
Martien Verbruggen | I used to have a Heisenbergmobile. Every time
| I looked at the speedometer, I got lost.
|


2008年8月8日星期五11:04:49 +1000,Martien Verbruggen

< mg ** @ tradingpost.com.auwrote:
On Fri, 8 Aug 2008 11:04:49 +1000, Martien Verbruggen
<mg**@tradingpost.com.auwrote:

> 2008年8月7日星期四17:14:29 - 0700,
Pilcrow< pi ***** @ pp.infowrote:
>On Thu, 07 Aug 2008 17:14:29 -0700,
Pilcrow <pi*****@pp.infowrote:

>>
这是一个快速程序,连同它的输出,说明了我认为是标准函数strtok的缺陷来自
>>
Here is a quick program, together with its output, that illustrates what
I consider to be a deficiency of the standard function strtok from


[snip]


[snip]


>我希望有一个默认值,当两个分隔符
(逗号,这里)相互邻接时返回。
>I would like there to be a default, to be returned when two delimiters
(the commas, here) abut each other.


我想我从上面了解到你有一个问题,
strtok()看到一系列分隔符作为单个
分隔符?


I think I understood from the above that you have a problem with
strtok() seeing a sequence of separator characters as a single
separator?


>有没有办法在不编写自己的函数的情况下获得此效果?
还有其他一些解决方法吗?
>Is there a way of getting this effect without writing my own function?
Is there some other workaround?


如果你不想使用C99功能,你可以考虑使用strcspn()创建
,这不会太难,或者你<如果你只有一个分隔符,可以使用strchr()。

Martien


If you don''t want to use C99 features, you could consider creating
something with strcspn(), which wouldn''t be too hard, or you
could use strchr() if you only have the one separator character.

Martien



谢谢,看起来我可以做一个这些。我应该能够自己解决这个问题。顺便说一句,您指的是C99的功能?

Thanks, looks like I could do one of these. I should have been able to
figure that out myself. BTW, what C99 features are you referring to?


2008年8月7日星期四18:30:09 -0700,

Pilcrow< pi ***** @ pp.infowrote:
On Thu, 07 Aug 2008 18:30:09 -0700,
Pilcrow <pi*****@pp.infowrote:

周五,2008年8月8日11:04:49 +1000,Martien Verbruggen < mg ** @ tradingpost.com.auwrote:
On Fri, 8 Aug 2008 11:04:49 +1000, Martien Verbruggen
<mg**@tradingpost.com.auwrote:

>> 2008年8月7日星期四17:14:29 -0700, Pilcrow< pi ***** @ pp.infowrote:
>>On Thu, 07 Aug 2008 17:14:29 -0700,
Pilcrow <pi*****@pp.infowrote:


>> ;如果你不想使用C99功能,你可以考虑使用strcspn()来创建
,这不会太难,或者如果你只是你可以使用strchr()有一个分隔符。
>>If you don''t want to use C99 features, you could consider creating
something with strcspn(), which wouldn''t be too hard, or you
could use strchr() if you only have the one separator character.


谢谢,看起来我可以做其中一个。我应该能够自己解决这个问题。顺便说一下,你指的是什么C99功能?
Thanks, looks like I could do one of these. I should have been able to
figure that out myself. BTW, what C99 features are you referring to?



我开始时使用strpbrk()编写一个响应,但是意识到

无论如何都不是正确的函数。当我编辑我的帖子时,我忘了

删除该引用。很抱歉这个混乱。


Martien

-

|

Martien Verbruggen |

|中只有10种类型的人世界;那些了解二元和那些

|的人谁不。

I started out by writing a response using strpbrk(), but realised that
that wasn''t the right function anyway. When I edited my post, I forgot
to remove that reference. Sorry for the confusion.

Martien
--
|
Martien Verbruggen | There are only 10 types of people in the
| world; those who understand binary and those
| who don''t.


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

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