일반적인 단어 사전에 대한 입력을 검사하고 입력이 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