ユーザーのテキストファイルと辞書を比較して、入力した単語が辞書に含まれているかどうかを確認するスペルチェックプログラムを作成しています。そうでない場合は、特定の単語が間違っていることをユーザーに知らせるエラーメッセージが表示されます。私は以下のコードのいくつかのバリエーションを試しましたが、望みの結果を得られませんでした。それはそれを投げ捨てている入れ子のwhileループの何かです。このコードはドラフト段階にあります。私はそれをより効率的なメモリなどにして整理しなければなりません。私はそれが最初に働くことに興味があります。ありがとう!2つのテキストファイルを比較する - Cのスペルチェックプログラム
編集:以下のヒントに従ってコードを少し変更しました。これで最初の単語が読み込まれ、辞書に入っていることが示されます。 2番目の単語が表示されますが、辞書スキャンループは実行されず、プログラムがハングします。私はそのネストされたwhileループが問題を引き起こしていることを知っています。
/*Spellcheck program*/
/*Author: */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
/*Open files and test that they open*/
FILE *fp1;
FILE *fp2;
char fname[20];
char wordcheck[45];/*The longest word in the English Language is 45 letters long*/
char worddict[45];
char dummy;
int i;
int dictcount = 0;
fp1 = fopen("dictionary.txt","r");
if (fp1 == NULL)
{
printf("The dictionary file did not open.");
exit(0);
}
printf("Please enter the path of the file you wish to check:\n");
scanf("%s", fname);
scanf("%c", &dummy);
fp2 = fopen(fname, "r");
if (fp2 == NULL)
{
printf("Your file did not open, please check your filepath and try again.\n");
printf("Please enter path of file you wish to check: \n");
scanf("%20s",fname);
fp2 = fopen(fname, "r");
}
else
{
printf("Your file opened correctly\n");
}
/*When files are open, read each word from the text file into an array:*/
while(fscanf(fp2,"%s", wordcheck)!=EOF)//Reads word from text file into array//
{
for (i=0; wordcheck[i]; i++)
{
wordcheck[i] = tolower(wordcheck[i]);//makes all characters lower case//
}
printf("%s", wordcheck);
while(dictcount >= 0)//reads dictionary word into array//
{
dictcount = 0;
fscanf(fp1,"%s", worddict);
if(strcmp(wordcheck, worddict)==0)//compare strings//
{
printf("This word: %s is in the dictionary\n", wordcheck);
break;
}
else
{
dictcount++;
}
if(worddict == NULL)
{
printf("Your word: %s is not in the dictionary\n", wordcheck);
}
}
dictcount++;
}
fclose(fp1);
fclose(fp2);
return 0;
}
終了しないでください。この男は才能を持っています。 – wildplasser
入力ワイルドカードをお寄せいただきありがとうございます。私はあなたが関数とファイルの最後までスキャンしていないが、真のstrcmpの下で休憩があると言っているものを取得します。それはEOFにスキャンを停止するのに十分ではないでしょうか? – adohertyd
ああ、そうです。 2番目の 'strcmp()'ブロックを別の場所に移動させるのは正当な理由です。 – wildplasser