C ++文件输入/输出搜索

时间:2012-11-29 11:02:07

标签: visual-c++ search file-io

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

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

感谢。

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

2 个答案:

答案 0 :(得分:1)

您需要fopen()来电中的文件的完整路径。

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

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

答案 1 :(得分:0)

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