【发布时间】:2013-07-27 14:49:54
【问题描述】:
我正在尝试使用strtok()。以下是我写的一段代码。它不起作用,但会无限打印", '"。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(){
char str[]="this, by the way, is a 'sample'";
char *tokens;
tokens = strtok(str, ", '");
//printf("%s\n",tokens);
//printf("%s\n", str);
while(tokens!=NULL)
{
printf("%s\n", tokens);
tokens = (NULL, ", '");
}
return 0;
}
以下是来自strtok() 手册页的代码,它运行良好。
#include <stdio.h>
#include <string.h>
int main ()
{
char str[] ="- This, a sample string.";
char * pch;
printf ("Splitting string \"%s\" into tokens:\n",str);
pch = strtok (str," ,.-");
while (pch != NULL)
{
printf ("%s\n",pch);
pch = strtok (NULL, " ,.-");
}
return 0;
}
我觉得我做了完全相同的事情。无法找出我的代码中的错误。谁能指出来。
【问题讨论】:
-
标出此题的副本完全错误!在这个问题中,OP忘记了编码中的函数名称,并且由于
,逗号运算符的行为没有产生错误而链接的问题是关于运算符的行为。