0

嗨,我从正在编写的程序中获取了以下代码,以根据字典以及其他验证检查用户生成的字符串。

我的问题是,虽然我的字典文件被正确引用,但程序给出了默认的“找不到字典”。我看不清楚我在这里做错了什么,如果有人有任何提示或指示,将不胜感激,

谢谢。

        //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
4

2 回答 2

1

您需要fopen()调用中文件的完整路径。

默认情况下,Visual Studio 将工作目录设置为存储解决方案文件的位置,这不一定是 .exe 所在的位置。

您可以将项目属性中的工作目录更改为$(SolutionDir)$(Configuration)\不需要路径。

于 2012-11-29T11:23:49.280 回答
0

解决方案是使用 strNcmp,这允许搜索单词的长度。使用 strcmp 时缓冲区中的空白空间会出现问题,这就是所谓的默认错误消息。

于 2012-12-05T15:05:43.383 回答