strtok()问题 [英] strtok() problem

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

问题描述

我正在这样使用它:


char * _command =" one two three four";

char * g_UserCommands [4] ;

const char * delimeters =" " ;;

g_UserCommands [0] = strtok(_command,delimeters);

g_UserCommands [1] = strtok(g_UserCommands [0],delimeters);

g_UserCommands [2] = strtok(g_UserCommands [1],delimeters);

g_UserCommands [3] = strtok(g_UserCommands [2],delimeters);

//然后我打印g_UserCommands的每个条目。


当我运行这个东西时,它会打印出一个一个......


我无法弄清楚我做错了什么...

解决方案

" ern" < ER ******* @ gmail.com>在消息中写道

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

char * _command =" one two three four";
char * g_UserCommands [4];
const char * delimeters =" " ;;
g_UserCommands [0] = strtok(_command,delimeters);
g_UserCommands [1] = strtok(g_UserCommands [0],delimeters);
g_UserCommands [2] = strtok(g_UserCommands [1],delimeters);
g_UserCommands [3] = strtok(g_UserCommands [2],delimeters);
//然后我打印g_UserCommands的每个条目。

当我运行这个东西,它只会打印一个一个......

我无法弄清楚我做错了什么......




第一次调用strtok()是用ptr来解析要解析的字符串。

对strtok()的所有后续调用都必须为NULL。

要开始解析下一个字符串,请使用新的非NULL ptr。


阅读手册。


Alex


感谢。那很有效。实际上,我确实阅读了手册,并且无法确定这个问题...因此问题。


阿列克谢A. Frounze写道:

" ern" < ER ******* @ gmail.com>在消息中写道
新闻:11 ********************** @ z14g2000cwz.googlegr oups.com ......

char * _command ="一二三四;


最好避免以_开头的标识符。其中一些可以在某些范围内使用,但更容易完全避免使用

来记住哪些可以在哪里使用。

char * g_UserCommands [4];
const char * delimeters =" " ;;
g_UserCommands [0] = strtok(_command,delimeters);
g_UserCommands [1] = strtok(g_UserCommands [0],delimeters);
g_UserCommands [2] = strtok(g_UserCommands [1],delimeters);
g_UserCommands [3] = strtok(g_UserCommands [2],delimeters);
//然后我打印g_UserCommands的每个条目。

当我运行这个东西,它只会打印一个一个......

我无法弄清楚我做错了什么......


<第一次调用strtok()是使用ptr来解析要解析的字符串。
对strtok()的所有后续调用必须为NULL。
要开始解析下一个字符串,请使用新的非NULL ptr。

阅读手册。




另一个问题是strtok修改了它标记的字符串和

C标准禁止您修改字符串文字。常见的

效果包括程序中止,发现字符串

文字已合并,所以当你更改一个实例时所有其他实例

更改和恶魔飞出你的鼻子然后用一个

湿腌鱼打你。


*从不*将字符串文字传递给strtok并且从不尝试以任何其他方式修改字符串

literal。


char命令[] ="一二三四;


会好得多。

-

Flash Gordon

生活在有趣的时代。

虽然我的电子邮件地址是垃圾邮件,但它是真实的,我读了它。


I''m using it like this:

char * _command = "one two three four";
char * g_UserCommands[4];
const char * delimeters = " ";
g_UserCommands[0] = strtok(_command, delimeters);
g_UserCommands[1] = strtok(g_UserCommands[0], delimeters);
g_UserCommands[2] = strtok(g_UserCommands[1], delimeters);
g_UserCommands[3] = strtok(g_UserCommands[2], delimeters);
//Then I print each entry of g_UserCommands.

When I run this thing, it will just print out one one one one...

I can''t figure out what I''m doing wrong...

解决方案

"ern" <er*******@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...

char * _command = "one two three four";
char * g_UserCommands[4];
const char * delimeters = " ";
g_UserCommands[0] = strtok(_command, delimeters);
g_UserCommands[1] = strtok(g_UserCommands[0], delimeters);
g_UserCommands[2] = strtok(g_UserCommands[1], delimeters);
g_UserCommands[3] = strtok(g_UserCommands[2], delimeters);
//Then I print each entry of g_UserCommands.

When I run this thing, it will just print out one one one one...

I can''t figure out what I''m doing wrong...



1st call to strtok() is with ptr to the string to be parsed.
All subsequent calls to strtok() must be with NULL.
To begin parsing next string, use a new non-NULL ptr.

Read The Manual.

Alex


Thanks. That worked. Actually, I did read the manual and couldn''t
figure it out... thus the question.


Alexei A. Frounze wrote:

"ern" <er*******@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...

char * _command = "one two three four";
It is best to avoid identifiers starting with an _. Some of them can be
used in some scopes, but it is far easier to completely avoid them that
to remember which ones can be used where.
char * g_UserCommands[4];
const char * delimeters = " ";
g_UserCommands[0] = strtok(_command, delimeters);
g_UserCommands[1] = strtok(g_UserCommands[0], delimeters);
g_UserCommands[2] = strtok(g_UserCommands[1], delimeters);
g_UserCommands[3] = strtok(g_UserCommands[2], delimeters);
//Then I print each entry of g_UserCommands.

When I run this thing, it will just print out one one one one...

I can''t figure out what I''m doing wrong...



1st call to strtok() is with ptr to the string to be parsed.
All subsequent calls to strtok() must be with NULL.
To begin parsing next string, use a new non-NULL ptr.

Read The Manual.



Another problem is that strtok modifies the string it is tokenising and
the C standard forbids you from modifying string literals. Common
effects of this include the program aborting, finding that string
literals have been merged so when you change one instance all the others
change, and demons flying out of your nose and then slapping you with a
wet kipper.

*Never* pass a string literal to strtok and never try to modify a string
literal in any other way.

char command[] = "one two three four";

would have been far better.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.


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

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