我正在尝试使用C来搜索文本文件,以查找始终位于特定位置的字符串。具体而言,我正在寻找三个连续的破折号。当它找到三条破折号时,它应该返回找到它的那一行。然后它应该继续到下一行并继续搜索三个连续的破折号,直到它到达文件的末尾。每次都应该打印行号。在文本文件中查找字符串C
这是我到目前为止有:
int main() {
FILE *f;
char inName[80];
printf("Read text from: ");
scanf(" %79[^\n]s\n", inName);
f = fopen(inName, "r");
if (f == 0) printf("ERROR: Cannot open file %s for reading.\n", inName);
int lineNumber = 0;
for(;;) {
char line[127];
fgets(line, 127, f);
if (!feof(f)) {
lineNumber++;
} else {
break;
}
double lf;
int d, d1;
char s[30];
char s1[4];
sscanf(line, " %d, %s, %s, %s, %s, %d, %d, %lf",
&d, &s, &s, &s, &s, &d, &s1, &lf);
if (s1 == "---") {
printf("%d\n", lineNumber); // what line
}
}
fclose(f);
return(0);
}
此代码运行,但不会显示任何信息。任何人都可以展示如何完成这项工作?谢谢:)
你试过调试器吗? – 2013-04-24 19:21:05