2016-10-02 188 views
-2

所以這是我的學校代碼,現在我的問題是在我的inputID函數中。如果評論說「如果相同!!!!!!!!!!!!!!!!!!!!!!!!!!!」,我會嘗試比較用戶給出的字符串和字符串存儲在我的數組字符串「IDArray」中。我嘗試使用strcmp函數,但我不斷收到錯誤。任何幫助,將不勝感激。它讀取的文本文件的內容顯示在代碼下面。謝謝比較字符串指針?比較字符串C

#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密碼2

ID33 PASSWORD3

ID44 PASSWORD4

ID55 PASSWORD5

ID55 PASSWORD5

+0

還請溫和我的代碼是多麼混亂:} –

+1

Nuke和重建。永遠不要以明文形式存儲密碼進行驗證。這是你說的任務。抱怨這項任務教會了非常不好的習慣。 – Joshua

+0

不,我知道這樣做的壞習慣。我還沒有應用散列函數。這是一個安全等級loll –

回答

0

你必須符合325(與註釋的行)一個錯字:這是你想要與之比較,不inputID這是它在函數的名稱IDInput

+0

OMG謝謝你,我討厭什麼時候搞砸你的代碼是最愚蠢的事情。 –

+0

@AndyStevenLlactahuamani是的,發生了。更有趣的是知道誰不想讓我告訴你? ;-) – deamentiaemundi