【问题标题】:comparing strings to pointers? Comparing strings in C比较字符串和指针?比较 C 中的字符串
【发布时间】:2016-10-02 23:45:56
【问题描述】:

这是我的学校作业代码,现在我的问题出在我的 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 密码1

ID22 密码2

ID33 密码3

ID44 密码4

ID55 密码5

ID55 密码5

【问题讨论】:

  • 另外,请不要对我的代码有多乱:}
  • 核弹并重建。切勿以明文形式存储密码以进行验证。你说的是任务。抱怨作业教会了真正的坏习惯。
  • 请尝试创建一个 MVCE (*.com/help/mcve)。此外,每当您想知道某事导致错误的原因时,您都需要提供完整的错误消息...
  • 另外,deamentiaemundi 正确诊断的实际问题是,您正在将函数指针与字符串进行比较。编译器应该对你尖叫“不兼容的类型”。如果是的话,别忘了提一下;那是你的问题。在你作为 C 程序员职业生涯的这个阶段,编译器不会出错,如果它给你一个警告,就需要修复它。你需要一段时间才能忽略警告——我很少忽略任何警告,但我编写 C 语言才 30 年,所以还有很多东西要学。
  • 哦,如果编译器没有对您的错误大喊大叫,您要么需要打开更多警告,要么需要获得更好的编译器。至少在没有警告的情况下,不应编译代码。如果我尝试在我的默认编译标志下编译它,它会失败(以及其他一些或多或少重要的问题)。

标签: c arrays string pointers strcmp


【解决方案1】:

您在第 325 行(带有 that 注释的行)有一个错字:您要比较的是 IDInput,而不是它所在函数的名称 inputID

【讨论】:

  • 天哪,谢谢你,我讨厌最愚蠢的事情会弄乱你的代码。
  • @AndyStevenLlactahuamani 是的,发生了。更有趣的是知道谁不想让我告诉你? ;-)
最近更新 更多