我有以下这段代码来检查输入与常用词的字典,并检查输入是否与 passHistory 文件中存储的先前输入匹配。我的问题是在 C 中比较字符串的 strcmp 方法似乎没有正确执行在我的代码中,因为如果在 passHistory 中已使用常用词或输入,则无法显示适当的错误。
一些指导将不胜感激。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#define MAX 30
#define gC_FOUND 99
#define gC_NOT_FOUND -99
int checkWordInFile(char * fileName,char * theWord);
int main()
{
char userString[MAX + 1];
int iResult;
printf("Enter your string: ");
gets(userString);
printf("\n\nYou entered: %s, please wait, checking in dictionary.\n\n", userString);
iResult = checkWordInFile("dictionary.txt",userString);
if( iResult == gC_FOUND )
{
printf("\nFound your word in the dictionary");
}
else
{
printf("\nCould not find your word in the dictionary");
}
iResult = checkWordInFile("passHistory.txt",userString);
if( iResult == gC_FOUND )
{
printf("\nPassword used");
}
else
{
printf("\nOk to use!");
}
printf("\n\n\n");
system("pause");
} /* end of main */
int checkWordInFile(char * fileName,char * theWord){
FILE * fptr;
char fileString[MAX + 1];
int iFound = -99;
//open the file
fptr = fopen(fileName, "r");
if (fptr == NULL)
{
printf("\nNo dictionary file\n");
printf("\n\n\n");
system("pause");
return (0); // just exit the program
}
/* read the contents of the file */
while( fgets(fileString, MAX, fptr) )
{
if( 0 == strcmp(theWord, fileString) )
{
iFound = -99;
}
}
fclose(fptr);
return(0);
}//end of checkwORDiNFile