2016-10-02 1 views
-2

Also das ist mein Code für eine Aufgabe für die Schule, und jetzt ist mein Problem in meiner InputID-Funktion. Wo der Kommentar sagt "Wenn das gleiche !!!!!!!!!!!!!!!!!!!!!!!!!!!", ich versuche, eine Zeichenfolge vom Benutzer und eine Zeichenfolge zu vergleichen in meinem Array von Strings "IDArray" gespeichert. Ich versuche, die strcmp Funktion zu benutzen, aber ich bekomme immer einen Fehler. Jede Hilfe mit diesem würde sehr geschätzt werden. Der Inhalt der Textdatei, aus der er gelesen wird, wird unterhalb des Codes angezeigt. Vielen Dank, dass SieZeichenfolgen mit Zeigern vergleichen? Vergleiche Strings in 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 PASSWORD2

ID33 kennwort3

ID44 PASSWORD4

ID55 password5

ID55 password5

+0

auch bitte sanft mit wie chaotisch mein Code ist:} –

+1

Nuke und Wiederaufbau. Speichern Sie Kennwörter NIEMALS im Klartext zur Überprüfung. Es ist eine Aufgabe, die du sagst. Beschweren Sie die Aufgabe lehrt wirklich schlechte Angewohnheiten. – Joshua

+0

Nein, ich verstehe, dass es eine schlechte Angewohnheit ist, das zu tun. Ich habe die Hash-Funktion noch nicht angewendet. Dies ist für eine Sicherheitsklasse loll –

Antwort

0

Sie haben einen Tippfehler in Zeile 325 (die Zeile mit dass Kommentar.): Es ist IDInput Sie vergleichen wollen, nicht inputID, die der Name der Funktion ist es in ist

+0

OMG Danke dafür, ich hasse es, wenn es die dümmsten Dinge sind, die deinen Code durcheinander bringen. –

+0

@AndyStevenLlactahuamani ja, das passiert. Interessanter ist es zu wissen, wer nicht will, dass ich dir das erzähle? ;-) – deamentiaemundi