【发布时间】:2018-04-24 23:54:21
【问题描述】:
我正在编写一个程序,我需要逐个令牌读取并检测某些关键字。其中一个关键字是“gt”,它代表大于。
我通过制表符、换行符、空格和回车将文本文件拆分为标记。缓冲区只是一个大字符数组。
char* word = strtok(buffer, " \n\t\r");
然后我有几个案例来检查可能的单词。 gt 如下。奇怪的是,这适用于其他关键字,有时甚至适用于其他出现的“gt”。
//gt
if(strcmp("gt", word) == 0){
type = GT;
literal_value = 0;
}
但是,尽管输入了“gt”,但仍无法到达。我注意到当我打印时,会发生这种情况
printf("WORD is %s!\n", word);
PRINTS "!ORD is gt"
这显然是不对的。如果答案很明显,请告诉我——这个错误已经躲避了我很长时间!
更新片段:
char * word = strtok(buffer, " \n\t\r");
while (word != NULL){
printf("word is %s!\n", sections); //PRINTS "!ORD is gt"
if(sections[0] == ';'){
break; //comment indicated by ';'
}
//gt
if(strcmp("gt", word) == 0){
type = GT;
literal_value = 0;
}
//...............
//other comparisons for less than, equal to
process(&curr, output_file); //function to process current token
word = strtok(NULL, " \n\t\r");
}
【问题讨论】:
-
我们甚至无法根据您提供的片段来解决这个问题。如果您想要一个有用的答案,那么您应该提供一个重现问题的minimal reproducible example。
-
你能把你写的整个代码片段提供给我们吗?