簡體   English   中英

在c的文本文件中對逐行文本應用二進制搜索

[英]Applying binary search in a text file in c for line by line text

我正在嘗試在文本文件中搜索單詞,但取得了一定的成功,但是代碼並不總是有效。 只是我不明白為什么它在循環中不起作用,但是當我手動執行時卻起作用。

我知道很多要看的東西,但是請有人幫我。

#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.

我不知道這是否應該執行二進制搜索,但這就是我所知道的。 我只是編程的初學者。

函數strcmp不會具體返回-11 (盡管可能會返回)。 返回值0< 0> 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");
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM