【发布时间】:2016-02-07 17:09:57
【问题描述】:
我需要使用 strtok 函数来分析某些字符串中的每个单词。 我写了这样的代码:
char *token;
token=strtok(string,symbol);
while(token!=NULL){
functionX(token); //this is a function that anlayze the token
token=strtok(NULL,symbol);
}
但是“functionX”只接收字符串的第一个单词和空指针。 如果我把
printf("%s",token);
而不是 functionX 它打印字符串的每一段。 我该如何解决这个问题?
这就是我所说的“functionX”:
void match(char *token, char *code){
FILE *rules;
char *tmp_token;
char stream[15];
int found;
rules=fopen("rules.vk","r");
found=0;
while((fgets(stream,sizeof(stream),rules))>0){
tmp_token=strtok(stream,";");
if((strcmp(tmp_token,token))==0){
strcpy(code,strtok(NULL,";"));
found=1;
}
}
if(found==0) strcpy(code,token);
}
【问题讨论】:
-
能显示“functionX”功能码吗?
-
@MohdShahril 是的。这是一个根据文件中写入的一些规则将每个标记与翻译相关联的功能。那是:pastebin.com/rBKi1Bx0
-
请发布
functionX的最小版本,其中显示问题中的错误。添加评论链接是没有用的 - 当它是死链接时更是如此。 -
@WeatherVane 抱歉,我是新来的。
-
你没有检查来自
fopen的返回值。这样做是必不可少的。然后你没有关闭文件。