【问题标题】:Applying binary search in a text file in c for line by line text在c中的文本文件中应用二进制搜索逐行文本
【发布时间】:2019-03-01 16:15:41
【问题描述】:

我正在尝试在文本文件中搜索一个单词,我有点成功,但代码并不总是有效。只是我不明白为什么它在循环中不起作用,但在我手动执行时起作用。

我知道有很多东西要看,但请任何人帮助我。

#include <stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>

void main()
{
    FILE *fp;
    fp=fopen("testdictionary.txt","r");

    char word[]="her";
    char line[7];
    int n;
    int upper_limit=48;
    int lower_limit=0;
    int result=-1;

    while(result!=0) {
        n=(upper_limit+lower_limit)/2;
        printf("Value of n:%d ",n);
        fseek(fp,n,SEEK_SET);

        // setting the file pointer to the beginning of the word. --
        fseek(fp,-1,SEEK_CUR);
        char tst;
        do {
            fseek(fp,-1,SEEK_CUR);
            if(ftell(fp)==0) {
                break;
            }

            tst=fgetc(fp);
            if(tst=='\n') {
                break;
            }

            fseek(fp,-1,SEEK_CUR);
        } while(tst!='\n');
        //----------------------------------------------------------

        fgets(line,7,fp);
        result=strcmp(line,strcat(word,"\n"));
        printf(" Result:%d ",result);

        if(result==1) {
            upper_limit=n;
            printf("Required 'word' is above the line of text.\n");
        }
        else if(result==-1) {
            lower_limit=n;
            printf("Required 'word' is below the line of text.\n");
        }
        else if(result==0) {
            printf("Word found");
        }
    }
}

我的文本文件

aoo
bpp
cas
dzx
edf
fvb
gty
her
iwe
jqw

输出(当我运行上述代码时。)

Value of n:24  Result:-1 Required 'word' is below the line of text.
Value of n:36  Result:-1 Required 'word' is below the line of text.
Value of n:1322  Result:1 Required 'word' is above the line of text.
Value of n:329639  Result:1 Required 'word' is above the line of text.
Value of n:84052197

我不明白的部分是,如果我手动输入 n=36,结果是 0 并且找到了单词。但是当我尝试自动搜索时,即使在第二步后 n 的值变为 36,循环不会中断,并且会给出奇怪而大的 n 值。

所以当我自己输入 n=36(如下所示)时,我得到了预期的输出,即找到了“她”这个词。

while(result!=0)
{
    // n=(upper_limit+lower_limit)/2;
    n=36;
    printf("Value of n:%d ",n);
    fseek(fp,n,SEEK_SET);

输出

Value of n:36  Result:0 Word found
Process returned 10 (0xA)   execution time : 0.141 s
Press any key to continue.

我不知道这是否是你应该做的二进制搜索,但这是我所知道的。我只是编程的初学者。

【问题讨论】:

  • 请不要张贴文字图片。将文本作为文本发布。
  • 请将您的输入和输出作为代码块添加到问题中,而不是显示屏幕截图。还显示预期的输出应该是什么样子。 “如果我手动输入 n=36”是什么意思 你在哪里输入这个?
  • 感谢@Jabberwocky 的建议。完成。
  • 感谢@Bodo。我已尝试提供更多详细信息。
  • @SandeshGhimire 你也应该努力,indent 你的代码正确。可读的代码对于查找错误最重要。

标签: c search text-files binary-search file-handling


【解决方案1】:

函数strcmp 不返回具体 -11(尽管它可能返回)。它返回值0&lt; 0&gt; 0

也在

result = strcmp(line, strcat(word, "\n"));

你不能将任何东西连接到

char word[] ="her";

因为数组没有空间。最好从文件字符串中删除换行符,而不是将其添加到目标字符串中。

即使可以,您也会在每次迭代中添加另一个换行符。所以我建议

fgets(line, 7, fp);
line [ strcspn(line, "\r\n") ] = '\0';      // truncate any newline
result = strcmp(line, word);
if(result > 0) {
    upper_limit = n;
    printf("Required 'word' is above the line of text.\n");
}
else if(result < 0) {
    lower_limit = n;
    printf("Required 'word' is below the line of text.\n");
}
else {   // no other possibility
    printf("Word found");
}

【讨论】:

  • \n 附加到word 时,此缓冲区溢出可能会覆盖变量n。当我将代码更改为使用 char word[]="her\n";result=strcmp(line,word); 时,它似乎按预期工作。
  • 现在程序按预期运行。我改变了你们建议的东西。感谢您的帮助。
猜你喜欢
  • 2016-02-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-02
  • 2021-04-20
  • 2012-04-18
  • 2018-11-16
  • 1970-01-01
相关资源
最近更新 更多