【发布时间】:2021-04-01 13:08:57
【问题描述】:
我编写了这个函数来搜索一个文本文件(目前只是说“立方体”)中的一个单词,然后调用相应的函数。但是,我收到“错误:未找到功能”。消息。
谁能帮我找出我的问题?
void read(){
const char cube[4] = "cube";
const char cone[4] = "cone";
const char sphere[6] = "sphere";
const char cylinder[8] = "cylinder";
char line[1024] ;
FILE* fp = fopen("instructions.txt", "r") ;
while (fgets(line , sizeof(line) , fp )!= NULL)
{
if (strstr(line , cube)!= NULL){
void cube();
}
else if (strstr(line , cone)!= NULL){
void cone();
}
else if (strstr(line , sphere)!= NULL){
void sphere();
}
else if (strstr(line , cylinder)!= NULL){
void cylinder();
}
else {
printf("Error: No Function Found./n");
}
}
fclose(fp);
free(line);
return;
}
【问题讨论】:
-
const char cube[4] = "cube"不是字符串。它不是空终止的。使用const char cube[] = "cube"并让编译器正确确定大小。 (提示:至少应为 5) -
OT :
free(line);非常错误。 -
@alexandrahowes 好的,但仍然需要进行此检查,因为如果您不这样做并且
fopen函数失败,您就麻烦了。 -
不...不,你不是。你根本没有给他们打电话。
-
正如 Lundin 在回答中指出的那样,如果你删除函数原型,你现在有
cube是一个字符数组,显然不能像函数一样调用它。
标签: c if-statement fopen strstr