入力された文字列をファイルで検索する必要がありますが、以下のコードは機能しません。常に「辞書に見つかりませんでした。ファイル(と呼ばれるDictionary.txt
)の内容は次のとおりです。
pow
jaw
pa$$word
コードは次のとおりです。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 30
main()
{
char inPassword[MAX + 1];
printf("\nEnter a password: ");
gets(inPassword);
printf("\n\nYou entered: %s, please wait, checking in dictionary.\n\n",inPassword);
checkWordInFile("Dictionary.txt",inPassword);
printf("\n\n\n");
system("pause");
}//end of main
void checkWordInFile(char *fileName, char *password);
{
char readString[MAX + 1];
FILE *fPtr;
int iFound = -1;
//open the file
fPtr = fopen(fileName, "r");
if (fPtr == NULL)
{
printf("\nNo dictionary file\n");
printf("\n\n\n");
system("pause");
exit(0); // just exit the program
}
while(fgets(readString, MAX, fPtr))
{
if(strcmp(password, readString) == 0)
{
iFound = 1;
}
}
fclose(fPtr);
if( iFound > 0 )
{
printf("\nFound your word in the dictionary");
}
else
{
printf("\nCould not find your word in the dictionary");
}
}