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 に答える