所以这是我的学校作业代码,现在我的问题出在我的inputID函数中。评论中写着“如果相同!!!!!!!!!!!!!!!!!!!!!!!!!!!”,我会尝试比较用户给出的字符串和字符串存储在我的字符串数组“IDArray”中。我尝试使用strcmp函数但是我一直收到错误。任何有关这方面的帮助将非常感激。它从中读取的文本文件的内容显示在代码下方。谢谢
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_RECORDS 1000
#define MAX_INPUT 40
void writeFile();
void inputPass();
void inputID();
void userInput();
void printDB();
void readFile();
void inputInit();
void DBInit();
void init();
FILE *fp;
char **IDArray;
char **passwordArray;
char *IDInput;
char *passInput;
int main()
{
init();
readFile();
printf("\n\n\tWelcome to CPS_633, Lab 1\t\n\n");
userInput();
writeFile();
printDB();
return 0;
}
void writeFile()
{
fp = fopen("Database_Table.txt", "w");
int i;
for (i = 0; i < MAX_RECORDS; i++)
{
if (IDArray[i][0] != '\0')
{
fprintf(fp, "%s\t%s\n", IDArray[i], passwordArray[i]);
}
else
{
break;
}
}
fclose(fp);
}
void printDB()
{
printf("\nUsername\tPassword\n");
int i;
int databaseLength = 0;
for (i = 0; i <= MAX_RECORDS; i++)
{
if (IDArray[i][0] != '\0')
{
printf("%s\t\t%s\n", IDArray[i], passwordArray[i]);
}
else
{
break;
}
}
}
void inputPass()
{
int correct = 1, strLength;
printf("Please Enter Your Password (no special characters): ");
fgets(passInput, MAX_INPUT, stdin);
strLength = strlen(passInput) - 1;
if (strLength > 0 && passInput[strLength] == '\n')
{
passInput[strLength] = '\0';
}
while (correct)
{
int k;
int specialCase = 1;
for (k = 0; k <= strLength; k++) //Searches for special characters
{
switch (passInput[k])
{
case '~':
printf("Special Character(s) Found\n");
specialCase = 0;
break;
case '!':
printf("Special Character(s) Found\n");
specialCase = 0;
break;
case '`':
printf("Special Character(s) Found\n");
specialCase = 0;
break;
case '@':
printf("Special Character(s) Found\n");
specialCase = 0;
break;
case '#':
printf("Special Character(s) Found\n");
specialCase = 0;
break;
case '$':
printf("Special Character(s) Found\n");
specialCase = 0;
break;
case '%':
printf("Special Character(s) Found\n");
specialCase = 0;
break;
case '^':
printf("Special Character(s) Found\n");
specialCase = 0;
break;
case '&':
printf("Special Character(s) Found\n");
specialCase = 0;
break;
case '*':
printf("Special Character(s) Found\n");
specialCase = 0;
break;
case '(':
printf("Special Character(s) Found\n");
specialCase = 0;
break;
case ')':
printf("Special Character(s) Found\n");
specialCase = 0;
break;
case '-':
printf("Special Character(s) Found\n");
specialCase = 0;
break;
case '_':
printf("Special Character(s) Found\n");
specialCase = 0;
break;
case '=':
printf("Special Character(s) Found\n");
specialCase = 0;
break;
case '+':
printf("Special Character(s) Found\n");
specialCase = 0;
break;
case '[':
printf("Special Character(s) Found\n");
specialCase = 0;
break;
case ']':
printf("Special Character(s) Found\n");
specialCase = 0;
break;
case '{':
printf("Special Character(s) Found\n");
specialCase = 0;
break;
case '}':
printf("Special Character(s) Found\n");
specialCase = 0;
break;
case '|':
printf("Special Character(s) Found\n");
specialCase = 0;
break;
case '\\':
printf("Special Character(s) Found\n");
specialCase = 0;
break;
case ':':
printf("Special Character(s) Found\n");
specialCase = 0;
break;
case ';':
printf("Special Character(s) Found\n");
specialCase = 0;
break;
case '"':
printf("Special Character(s) Found\n");
specialCase = 0;
break;
case ',':
printf("Special Character(s) Found\n");
specialCase = 0;
break;
case '<':
printf("Special Character(s) Found\n");
specialCase = 0;
break;
case '.':
printf("Special Character(s) Found\n");
specialCase = 0;
break;
case '>':
printf("Special Character(s) Found\n");
specialCase = 0;
break;
case '?':
printf("Special Character(s) Found\n");
specialCase = 0;
break;
case '/':
printf("Special Character(s) Found\n");
specialCase = 0;
break;
}
if (specialCase == 0)
break;
}
if (specialCase != 0)
{
if (strLength > 12) //Password longer than 12 characters
{
printf("This password is too long, the characters after the 12th has been cut off\n");
int i;
for (i = 0; i < MAX_RECORDS; i++)
{
if (passwordArray[i][0] == '\0')
{
strncpy(passwordArray[i], passInput, 12);
break;
}
else
{
continue;
}
}
correct = 0;
}
else //Password shorter than 12 characters
{
int i, j;
for (j = strLength; j <= 12; j++) //Pads password with "-"
{
passInput[j] = '-';
}
for (i = 0; i < MAX_RECORDS; i++) // Traverses array for first empty slot
{
if (passwordArray[i][0] == '\0') //If empty insert
{
strncpy(passwordArray[i], passInput, 12);
break;
}
else // If not empty continue
{
continue;
}
}
correct = 0;
}
}
else
{
printf("Please Enter Your Password (no special characters): ");
fgets(passInput, MAX_INPUT, stdin);
strLength = strlen(passInput) - 1;
if (strLength > 0 && passInput[strLength] == '\n')
{
passInput[strLength] = '\0';
}
}
}
}
void inputID()
{
int correct = 1, strLength;
printf("Please Enter Your Username ID: ");
fgets(IDInput, MAX_INPUT, stdin);
strLength = strlen(IDInput) - 1;
if (strLength > 0 && IDInput[strLength] == '\n')
IDInput[strLength] = '\0';
while (correct)
{
if (strLength > 32) //If longer than 32 characters
{
printf("This Username ID is longer than 32 characters\n");
printf("Please Enter Your Username ID: ");
fgets(IDInput, MAX_INPUT, stdin);
strLength = strlen(IDInput) - 1;
if (strLength > 0 && IDInput[strLength] == '\n')
IDInput[strLength] = '\0';
}
else if (strLength < 4) //If shorter than 4 characters
{
printf("This Username ID is shorter than 4 characters\n");
printf("Please Enter Your Username ID: ");
fgets(IDInput, MAX_INPUT, stdin);
strLength = strlen(IDInput) - 1;
if (strLength > 0 && IDInput[strLength] == '\n')
IDInput[strLength] = '\0';
}
else //If acceptable length
{
int i;
for (i = 0; i < MAX_RECORDS; i++)
{
if (IDArray[i][0] != '\0') //If element occupied, compare
{
if (strcmp(IDArray[i], inputID) == 0) // If the same!!!!!!!!!!!!!!!!!!
{
printf("Found Match");
correct = 0;
break;
}
else //If not the same
{
continue;
}
}
else //If element empty, insert
{
strcpy(IDArray[i], IDInput);
break;
}
}
correct = 0;
}
}
}
void userInput()
{
inputID();
inputPass();
}
void readFile()
{
fp = fopen("Database_Table.txt", "r");
char line[MAX_INPUT];
if (fp == NULL)
{
perror("Error in opening file");
}
else
{
int i = 0;
while (!feof(fp))
{
if (fgets(line, sizeof(line), fp) == NULL)
{
break;
}
else
{
sscanf(line, "%s\t%s", IDInput, passInput);
strcpy(IDArray[i], IDInput);
strcpy(passwordArray[i], passInput);
i++;
}
}
}
fclose(fp);
}
void inputInit()
{
IDInput = (char *)malloc(sizeof(char) * MAX_INPUT);
passInput = (char *)malloc(sizeof(char) * MAX_INPUT);
}
void DBInit()
{
IDArray = (char **)malloc(sizeof(char *) * MAX_RECORDS);
passwordArray = (char **)malloc(sizeof(char *) * MAX_RECORDS);
int i, j;
for (i = 0; i < MAX_RECORDS; i++)
{
IDArray[i] = (char *)malloc(sizeof(char) * MAX_INPUT);
passwordArray[i] = (char *)malloc(sizeof(char) * MAX_INPUT);
for (j = 0; j < MAX_INPUT; j++)
{
IDArray[i][j] = '\0';
passwordArray[i][j] = '\0';
}
}
}
void init()
{
DBInit();
inputInit();
}
ID11 PASSWORD1
ID22 PASSWORD2
ID33 PASSWORD3
ID44 PASSWORD4
ID55 PASSWORD5
ID55 PASSWORD5
答案 0 :(得分:0)
你在第325行( 评论的行)中有一个拼写错误:你要与IDInput
进行比较,而不是inputID
这是函数的名称它在。