我有下面的這段代碼來檢查輸入與普通單詞字典,並檢查輸入是否匹配存儲在passHistory文件中的先前輸入。我的問題是,strcmp方法比較C中的字符串在我的代碼中似乎沒有正確執行,因爲如果在passHistory中已經使用了常用單詞或輸入,它將無法顯示相應的錯誤。C輸入檢查和以前的輸出驗證代碼
一些指導將不勝感激。
#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
你怎麼知道'strcmp'方法工作不正常? – PearsonArtPhoto