2011-12-23 5 views
1

辞書とユーザーテキストファイルを読み込んでチェックする簡単なスペルチェッカーを作った。プログラムは、辞書にない単語の行と単語のインデックスを表示する必要があります。だから、ユーザーのテキストファイルが返されるまで\n文字(段落または文の末尾に)が正しく機能します。だからこんにちは、実際にはHello\nとして辞書に対してテストされ、プログラムは間違っていると考えています。誰でも\n文字を削除する方法をアドバイスできますか?ここに私のコードです:Cの n文字の呪い

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

void StrLower(char str[]) 
{ 
    int i; 

    for (i = 0; str[i] != '\0'; i++) 
     str[i] = (char)tolower(str[i]); 
    } 

int main (int argc, const char * argv[]) { 
    FILE *fpDict, *fpWords; 

    fpWords = fopen(argv[2], "r"); 
    if((fpDict = fopen(argv[1], "r")) == NULL) { 
     printf("No dictionary file\n"); 
     return 1; 
    } 

    char dictionaryWord[50]; // current word read from dictionary 
    char line[100]; // line read from spell check file (max 50 chars) 
    int isWordfound = 0; // 1 if word found in dictionary 
    int lineCount = 0; // line in spellcheck file we are currently on 
    int wordCount = 0; // word on line of spellcheck file we are currently on 

    while (fgets (line, sizeof line, fpWords) != NULL) 
    { 
     lineCount ++; 
     wordCount = 0; 
     char *spellCheckWord; 
     spellCheckWord = strtok(line, " "); 
     while (spellCheckWord != NULL) { 
      wordCount++; 
      spellCheckWord = strtok(NULL, " ,"); 

      if(spellCheckWord==NULL) 
       continue; 

      StrLower(spellCheckWord); 
      printf("'%s'\n", spellCheckWord); 


      while(!feof(fpDict)) 
      { 
       fscanf(fpDict,"%s",dictionaryWord); 
       int res = strcmp(dictionaryWord, spellCheckWord); 

       if(res==0) 
       { 
        isWordfound = 1; 
        break; 
       } 
      } 

      if(!isWordfound){ 
       printf("word '%s' not found in Dictionary on line: %d, word index: %d\n", spellCheckWord, lineCount, wordCount); //print word and line not in dictionary 
      } 


      rewind(fpDict); //resets dictionarry file pointer 
      isWordfound = 0; //resets wordfound for next iteration 

     } 
    } 

    fclose(fpDict); 
    fclose(fpWords); 
    return 0; 
} 

すごくお返事ありがとうございました。あなたたちは素晴らしく、月の上にそれがあります!

+0

これは質問の範囲外ですが、あなたのコードが間違っている:それはありません行の最初の単語をチェックしないでください。これは、単語のチェックの前に再度(strtokを使用して)トークン化しているためです。チェックの後、strtokが現在あるループの終わりにそれを行うべきです。 – Adonais

答えて

2

strtokに渡す引数に\ nを追加してみてください。

+1

あるいは、 'strtok'が途切れている(必ずしも必要ではありませんが、個人的に使用することはありません)、' isspace'(あるいはおそらく '!isalpha')を使って空白の衝突を避けてください。 –

+0

'strtok(行、" \ n ")'は '' \ n "'だけであれば '' \ n ''を' '行から削除しません。 – chux

1

単純に比較のために文字を削除し、それが行の最後にあることがわかっている場合は、バッファに単語を読み込んだときに\nstrchr()を実行し、その位置を置き換えます見つけた場合は\0となります。

+0

'fgets()'の結果は必ずしも '" \ n "'で終わるとは限りません。長い行は部分的にしか読み込まれません。ファイル内の最後の行は、どちらでもない可能性があります。 'strchr()'は 'NULL'の戻り値のテストが追加されたときに機能します。 – chux

1

方法について:

size_t length = strlen(dictionaryWord); 
if (length > 0 && dictionaryWord[length-1] == '\n') { 
    dictionaryWord[length-1] = 0; 
} 
+0

ニース:( '' dictionaryWord [ - length] = 0; ''は、後のコードに便利です)。どちらも仕事を終わらせる。 – chux

5

削除「\をn」のすぐfgets()呼び出し後:

while (fgets (line, sizeof line, fpWords) != NULL) 
{ 
    size_t linelen = strlen(line); 
    assert((linelen > 0) && "this can happen only when file is binary"); 
    if (line[linelen - 1] == '\n') line[--linelen] = 0; /* remove trailing '\n' and update linelen */