【发布时间】:2011-01-31 06:19:04
【问题描述】:
所以我的代码不起作用...
test.c:27: warning: passing argument 1 of ‘search’ from incompatible pointer type
这是 fgets 行。
我的代码打开一个文件,逐行读取文件,我正在尝试创建一个“搜索”函数,该函数将返回一个值,该值指示是否在文件的该行上找到了该字符串。
我的最终目标是实现一个搜索和替换程序。但是一步一步,嗯? 这是我目前所拥有的:
#include <stdio.h>
#include <string.h>
int search(const char *content[], const char *search_term)
{
int t;
for(t=0; content[t]; ++t){
if(!strcmp(content[t], search_term)){
return t; // found
}
}
return 0; // not found
}
int main(int argc, char *argv[])
{
FILE *file;
char line[BUFSIZ];
int linenumber=0;
char term[20] = "hello world";
file = fopen(argv[1], "r");
if(file != NULL){
while(fgets(line, sizeof(line), file)){
if(search(line, term) != -1){
printf("Search Term Found!!\n");
}
++linenumber;
}
}
else{
perror(argv[1]);
}
fclose(file);
return 0;
}
【问题讨论】:
-
错误信息告诉你问题出在 search() 调用上,而不是 fgets() 调用上。注意你的编译器;它正在努力帮助你。
-
开始代理调试!