C++ file input/output search
- by Brian J
Hi I took the following code from a program I'm writing to check a user generated string against a dictionary as well as other validation.
My problem is that although my dictionary file is referenced correctly,the program gives the default "no dictionary found".I can't see clearly what I'm doing in error here,if anyone has any tips or pointers it would be appreciated,
Thanks.
//variables for checkWordInFile
#define gC_FOUND 99
#define gC_NOT_FOUND -99
//
static bool certifyThat(bool condition, const char* error) {
if(!condition) printf("%s", error);
return !condition;
}
//method to validate a user generated password following password guidelines.
void validatePass()
{
FILE *fptr;
char password[MAX+1];
int iChar,iUpper,iLower,iSymbol,iNumber,iTotal,iResult,iCount;
//shows user password guidelines
printf("\n\n\t\tPassword rules: ");
printf("\n\n\t\t 1. Passwords must be at least 9 characters long and less than 15 characters. ");
printf("\n\n\t\t 2. Passwords must have at least 2 numbers in them.");
printf("\n\n\t\t 3. Passwords must have at least 2 uppercase letters and 2 lowercase letters in them.");
printf("\n\n\t\t 4. Passwords must have at least 1 symbol in them (eg ?, $, £, %).");
printf("\n\n\t\t 5. Passwords may not have small, common words in them eg hat, pow or ate.");
//gets user password input
get_user_password:
printf("\n\n\t\tEnter your password following password rules: ");
scanf("%s", &password);
iChar = countLetters(password,&iUpper,&iLower,&iSymbol,&iNumber,&iTotal);
iUpper = countLetters(password,&iUpper,&iLower,&iSymbol,&iNumber,&iTotal);
iLower =countLetters(password,&iUpper,&iLower,&iSymbol,&iNumber,&iTotal);
iSymbol =countLetters(password,&iUpper,&iLower,&iSymbol,&iNumber,&iTotal);
iNumber = countLetters(password,&iUpper,&iLower,&iSymbol,&iNumber,&iTotal);
iTotal = countLetters(password,&iUpper,&iLower,&iSymbol,&iNumber,&iTotal);
if(certifyThat(iUpper >= 2, "Not enough uppercase letters!!!\n")
|| certifyThat(iLower >= 2, "Not enough lowercase letters!!!\n")
|| certifyThat(iSymbol >= 1, "Not enough symbols!!!\n")
|| certifyThat(iNumber >= 2, "Not enough numbers!!!\n")
|| certifyThat(iTotal >= 9, "Not enough characters!!!\n")
|| certifyThat(iTotal <= 15, "Too many characters!!!\n"))
goto get_user_password;
iResult = checkWordInFile("dictionary.txt", password);
if(certifyThat(iResult != gC_FOUND, "Password contains small common 3 letter word/s."))
goto get_user_password;
iResult = checkWordInFile("passHistory.txt",password);
if(certifyThat(iResult != gC_FOUND, "Password contains previously used password."))
goto get_user_password;
printf("\n\n\n Your new password is verified ");
printf(password);
//writing password to passHistroy file.
fptr = fopen("passHistory.txt", "w"); // create or open the file
for( iCount = 0; iCount < 8; iCount++)
{
fprintf(fptr, "%s\n", password[iCount]);
}
fclose(fptr);
printf("\n\n\n");
system("pause");
}//end validatePass method
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