Visual c++ C++;文件输入/输出搜索
您好,我从我正在编写的程序中获取了以下代码,用于根据字典检查用户生成的字符串以及其他验证 我的问题是,虽然我的字典文件被正确引用,但程序给出了默认的“找不到字典”。我看不清楚我在这里犯了什么错误,如果有人有任何提示或指针,我将不胜感激 谢谢Visual c++ C++;文件输入/输出搜索,visual-c++,search,file-io,Visual C++,Search,File Io,您好,我从我正在编写的程序中获取了以下代码,用于根据字典检查用户生成的字符串以及其他验证 我的问题是,虽然我的字典文件被正确引用,但程序给出了默认的“找不到字典”。我看不清楚我在这里犯了什么错误,如果有人有任何提示或指针,我将不胜感激 谢谢 //variables for checkWordInFile #define gC_FOUND 99 #define gC_NOT_FOUND -99 // sta
//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
//checkWordInFile的变量
#定义找到的gC\u 99
#定义未找到的gC\u-99
//
静态布尔证明(布尔条件,常量字符*错误){
如果(!条件)printf(“%s”,错误);
返回!条件;
}
//方法按照密码准则验证用户生成的密码。
void validatePass()
{
文件*fptr;
字符密码[MAX+1];
int iChar、iUpper、iLower、iSymbol、iNumber、iTotal、iResult、iCount;
//显示用户密码准则
printf(“\n\n\t\t密码规则:”);
printf(“\n\n\t\t 1.密码长度必须至少为9个字符且少于15个字符。”);
printf(“\n\n\t\t 2.密码中必须至少包含2个数字。”);
printf(“\n\n\t\t 3.密码中必须至少包含2个大写字母和2个小写字母。”);
printf(“\n\n\t\t 4.密码中必须至少有一个符号(例如?,$,£,%”);
printf(“\n\n\t\t 5.密码中不能有小的常用词,例如hat、pow或ate。”);
//获取用户密码输入
获取用户密码:
printf(“\n\n\t\t按照密码规则输入密码:”);
scanf(“%s”和密码);
iChar=计数字母(密码、iUpper、iLower、iSymbol、iNumber和iTotal);
iUpper=countLetters(密码,&iUpper,&iLower,&iSymbol,&iNumber,&iTotal);
iLower=countLetters(密码、&iUpper、&iLower、&iSymbol、&iNumber、&iTotal);
iSymbol=countLetters(密码、&iUpper、&iLower、&iSymbol、&iNumber、&iTotal);
iNumber=countLetters(密码、&iUpper、&iLower、&iSymbol、&iNumber、&iTotal);
iTotal=countLetters(密码、&iUpper、&iLower、&iSymbol、&iNumber、&iTotal);
如果(证明(iUpper>=2,“大写字母不够!!!\n”)
||证明(iLower>=2,“小写字母不够!!!\n”)
||证明(iSymbol>=1,“符号不足!!!\n”)
||证明(iNumber>=2,“数字不够!!!\n”)
||证明(iTotal>=9,“字符不够!!!\n”)
||证明(iTotal您需要在fopen()
调用中找到文件的完整路径
默认情况下,Visual Studio将工作目录设置为存储解决方案文件的位置,而不一定是.exe所在的位置
您可以将项目属性中的工作目录更改为$(SolutionDir)$(配置)\
则不需要路径。解决方案是使用strNcmp,这允许搜索单词的长度。问题出现在使用strcmp的缓冲区中的空白处,这就是所谓的默认错误消息。您是否尝试在调试器中单步执行代码?以下操作不起作用,因为这是一个问题用strcmp应该是strncmp,但是当我改成strncmp时,我仍然得到同样的错误,有什么想法吗?