C语言实现文本文档查找,存在bug

用c写了一段文本文档查找的代码,输入被查找文件和关键字后,可以确定被查找文件中是否含有关键字,出现了几次,以及每次出现的行与列(字符串首字母的行与列,注:不支持跨行查找)。如查找单词happy,文件中出现了一次,结果应输出:1次;m行;n列 (即字母'h'的坐标)

但在测试的过程中,在个别情况下,对列的定位不准确,其余,如出现次数,行均正确,但对有时列为何不正确没能找出原因。

现在主要想找出代码哪里有问题,为什么有时候对列的确定出问题,代码后附上我的测试文件、用例以及出错的地方。

输入文件名处代码的逻辑还有些问题,但暂时先不管。

代码如下:

 #define _CRT_SECURE_NO_WARNINGS
#define LENGTH 81
#define MAX1 253
#define MAX2 260
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void mygrep(FILE *ptr1, char *str, FILE *ptr2);

int main()
{
FILE *p_source = NULL;
FILE *p_result = NULL;
char source[MAX1] = { 0 };
char result[MAX2] = { 0 };
char key_word[LENGTH] = { 0 };  

/*打开被查找文件*/
while (1)
{
printf("请输入被查找文件路径及名称:\n如:C:\\...\\FileName\n(注:长度应不大于253个字符)\n");
scanf("%s", source);
if ((p_source = fopen(source, "r")) != NULL)
{
break;
}
else if(strlen(source) > 253)/*检查字符数量*/
{
printf("已超过253个字符\n");
continue;
}
else
{
printf("文件不存在\n");
continue;
}
}

/*命名结果文件并打开*/
sprintf(result, "%s_result", source);
p_result = fopen(result, "w");

/*输入关键字*/
while(1)
{
printf("请输入关键字:(不超过80个字符)\n");
scanf("%s", key_word);
if(strlen(key_word) > 80)/*检查字符数量*/
{
printf("已超过80个字符\n");
continue;
}
else
break;
}

mygrep(p_source, key_word, p_result);

fclose(p_source);
fclose(p_result);

system("pause");
return 0;
}

void mygrep(FILE *ptr1_move, char *str, FILE *ptr2)
{
char tmp = '0';  /*存放文件指针当前读取的字符*/
int line = 1;
int column = 1;
int n = 0;      /*连续匹配关键字字符数量*/
int count = 0;

if(ptr1_move == NULL)
{
printf("错误!\n");
exit(1);
}

while(tmp != EOF)
{
tmp = fgetc(ptr1_move);

if(tmp == '\n')
{
line++;
column = 1;
}
else if(tmp == str[n])
{
n++;
column++;
if(tmp == str[strlen(str) - 1] && n == strlen(str))
{
count++;
n = 0;
printf("行数 : %3d , 列数 : %3d\n", line, column - strlen(str));
fprintf(ptr2, "行数 : %3d , 列数 : %3d\n", line, column - strlen(str));
}
}
else
{
fseek(ptr1_move, -n, 1);
column = column - n + 1;
n = 0;
}   
}

fprintf(ptr2, "\n内容:%s\n", str);
printf("\n内容:%s\n", str);
fprintf(ptr2, "\n出现次数:%d\n", count);
printf("\n出现次数:%d\n\n", count);
}

测试文件(网上找的一段英语作文),如下:
In my dual profession as an educator and health care provider, I have worked with numerous children infected with there virus
that causes AIDS. The relationships that I have had with these special kids have been gifts in my life. They have taught me
so many things, but I have especially learned that great courage can be found in the smallest of packages. Let me tell you
about Tyler.aaand

Tyler was born infected with HIV: his mother was also infected. From the very beginning of his life, he was dependent on
medications to enable him to survive. When he was five, he had a tube surgically inserted in a vein in his chest. This tube
was connected to a pump, which he carried in a small backpack on his back. Medications were hooked up to this pump and were
continuously supplied through this tube to his bloodstream. At times, he also needed supplemented oxygen to support his
breathing.

Tyler wasn’t willing to give up one single moment of his childhood to this deadly disease. It was not unusual to find him
playing and racing around his backyard, wearing his medicine-laden backpack and dragging his tank of oxygen behind him in
his little wagon. All of us who knew Tyler marveled at his pure joy in being alive and the energy it gave him. Tyler’s mom
often teased him by telling him that he moved so fast she needed to dress him in red. That way, when she peered through the
window to check on him playing in the yard, she could quickly spot him.

This dreaded disease eventually wore down even the likes of a little dynamo like Tyler. He grew quite ill and,
unfortunately, so did his HIV-infected mother. When it became apparent that he wasn’t going to survive, Tyler’s mom talked
to him about death. She comforted him by telling Tyler that she was dying too, and that she would be with him soon in heaven.

A few days before his death, Tyler beckoned me over to his hospital bed and whispered, “I might die soon. I’m not scared.
When I die, please dress me in red. Mom promised she’s coming to heaven, too. I’ll be playing when she gets there, and I
want gets there, to make surthe she can find me.”

用例: the

出现错误的地方:倒数第二行when I die...那一行,最后一个there中包含的the,列的输出有误。

请大家帮帮忙,到底是哪里有问题

3个回答

像这种数据问题,最好用二进制格式打开分析,因为很多格式会有后期处理,再显示出来,影响你的直观判断。

  When I die, please dress me in red. Mom promised she’s coming to heaven, too. I’ll be playing when she gets there, and I

    这里只有 there 符合标准,不知道你说的错是什么?

你可以把文件放在txt当中,使用VS来打开看,就可以了。。(看行和列)

zcmerlin
zcmerlin 问题找到了,txt打开后there前的两个 ' 符号是全角
3 年多之前 回复

运行结果:
行数 : 1 , 列数 : 115
行数 : 2 , 列数 : 58
行数 : 3 , 列数 : 82
行数 : 5 , 列数 : 41
行数 : 5 , 列数 : 70
行数 : 12 , 列数 : 88
行数 : 13 , 列数 : 121
行数 : 14 , 列数 : 35
行数 : 15 , 列数 : 48
行数 : 16 , 列数 : 42
行数 : 19 , 列数 : 111
行数 : 20 , 列数 : 11
行数 : 20 , 列数 : 29
UE中there就是19行111列,你出错是指什么

zcmerlin
zcmerlin 回复yuezhu165: 我用的notepad++,txt格式打开,不过问题找到了,那个there前有两个 ' 符号,是全角
3 年多之前 回复
yunda333
yuezhu165 t\nhe th\ne会出错,换行后n应该清零
3 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐