zcmerlin 2017-01-21 14:53 采纳率: 70%
浏览 1350
已采纳

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条回答 默认 最新

  • dcy_xm 2017-01-25 00:49
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 我想在一个软件里添加一个优惠弹窗,应该怎么写代码
  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流