0

ファイル内の文字列を検索する小さなプログラムがあります。この文字列の末尾には可変部分があり、常にサイズを示すバイトが先行します。

たとえば、「aaaaa.http://www.example.combbbbb」の「http://」を探します (「.」の ASCII コードは 0x17.

ファイルを開いたとしましょう。実行されるコードは次のとおりです。

while(car != EOF){
    car = fgetc(file[ii]); // we get everything in the file
    lastBuffStart=ftell(file[ii]);
    ij=1;
    buffer[0]=car; // we start editing the buffer
    printf("\n%d (%c) - %d (%c) ",car,car,base[0],base[0]);
    while(ij<(buffsize-1)){
         buffer[ij]=fgetc(file[ii]);
         printf("\n | %d (%c) - %d (%c) ",buffer[ij],buffer[ij],base[ij],base[ij]);
         ij++;
    }

    fseek(file[ii],lastBuffStart,0); // we get back to the old position before the buffer continues

    if(strcmp(buffer,base)==0){ // we compare
         byteSize = (ftell(file[ii])-1); // we get the position of the size byte
         printf("\nFound : 0x%x\n",byteSize);
         }
    }

すべてのファイルを読み取り、ベース (http://) と比較する次の文字をバッファーに入れます。

私の問題は、 printf("\n | %d (%c) - %d (%c) ",buffer[ij],buffer[ij],base[ij],base[ij]); を削除した場合です。何も見つかりません...

私は自分が間違っていることを本当に見ることができません。

手伝って頂けますか ?

前もって感謝します。

4

1 に答える 1

3

バッファを null で終了するのを忘れました。または、strcmp の代わりに memcmp を使用する必要があります。また、while ループの代わりに fread を使用すると、コードがより明確になります。

于 2011-06-26T07:44:56.177 に答える