【问题标题】:String array comparing with user string gives access error字符串数组与用户字符串比较给出访问错误
【发布时间】:2016-07-01 10:34:09
【问题描述】:

我正在尝试构建一个程序,该程序将从文件中获取登录详细信息(用户名和密码)的list,并允许您选择输入用户名和密码,并将其与批准的登录名和结果进行比较给出。在我的strcmp 中,我收到了access violation error 0xC0000005

#include <stdio.h>
#include <conio.h>
#include<stdlib.h>

FILE *fptr;
void main();
void openFile();
void closeFile();
char approvedUsrnames[3][6];
char approvedPassword[3][6];


void main()
{
    char userPassword[6], usrname[6], inputChar, fileString[6];
    int i;
    openFile();

    int numofLogins= 3;


    if (fptr != NULL)
    {
        printf("\nReading file with scanf\n");
        while (!feof(fptr))
        {
            for (i = 0; i < 3; i++) {
                fgets(approvedUsrnames[i], 6, fptr);
                fgets(approvedPassword[i], 6, fptr);
            }

        }
        closeFile();
    }


    printf("Enter User name: ");
    scanf("%s",usrname);
    printf("Enter the password <any 6 characters>: ");

    for (i = 0; i<6; i++)
    {
        inputChar = _getch();
        userPassword[i] = inputChar;
        inputChar = '*';
        printf("%c", inputChar);
    }//obfuscate the input to the user

    /*If you want to know what you have entered as password, to be removed*/
    printf("\nYour password is %s:", userPassword);


    for (i = 1; i < 3; i++) {
        printf("\Username is good %s\n", approvedUsrnames[i]);

        if (strcmp(approvedUsrnames[i], usrname  == 0)){
            printf("\Username is good\n");
            if (strcmp(userPassword, approvedPassword[i]) == 0) {
                printf("\nPassword is good\n");
            }//end nested if
            else {
                printf("\nPassword is not match\n");
            }
        }//end if
    }//end for

    _getch();

}
void openFile()
{
    fptr = fopen("approvedLogins.dat", "r");
    if (fptr == NULL)
    {
        printf("Error opening file ! \n");
    }
    else {
        printf("Login file read successfully ! \n");
    }
}
void closeFile()
{
    fclose(fptr);
}

【问题讨论】:

  • userPassword 不是 NUL 终止的。
  • @kaylum 你能解释一下你的意思以及我将如何解决它吗?
  • 您正在使用userPassword 作为字符串(例如在strcmp 中)。 C 中的字符串必须有一个 \0 字符作为数组中的最后一个字节。您的 userPassword 有 6 个字节的非 NUL 数据,因此不是有效的 C 字符串。一种解决方法是为 NUL 分配额外空间并显式初始化变量:char userPassword[7]="";`
  • 添加一个字符并初始化字符串会在 strcmp 上产生相同的错误
  • 好吧,你打错了:strcmp(approvedUsrnames[i], usrname == 0)==0 错误地位于 strcmp 中。

标签: c arrays file


【解决方案1】:

@anthonygordon

  1. 字符串 usrname 和 userPassword 很可能没有以 '\0'(空终止符)终止 a) 因为 scanf() 可能会尝试填写比 usrname 可以捕获的字符更多的字符 b) for 循环只需迭代 5 次,以便将 userPassword[5] 显式设置为 '\0'。 (如果期望使用 6 个字符长的用户名和密码,则将数组的长度从 6 更改为 7,并确保 array[6] 填充为 '\0')(记住在 'C' 数组下标为 0 ..(arraysize-1))

  2. 另外,一旦用户名和密码匹配,就退出循环。

【讨论】:

    【解决方案2】:

    您为用户名和密码字符数组选择了特定的长度。这意味着您知道要比较的字符数量。

    在这种情况下,您不需要在字符数组中添加“/0”。只需使用 strncmp() 函数并在第二个参数中指定要比较的字符数。

    见:http://www.tutorialspoint.com/c_standard_library/c_function_strncmp.htm

    只是一个提示: 在您添加的每一段代码之后进行测试。这样很容易知道是什么代码导致了问题。只有当你真正有经验(可能是 4 年以上的编码)时,你才能勇敢地在测试之前编写更多的代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-06
      • 2014-11-28
      • 2012-05-16
      • 2012-10-31
      相关资源
      最近更新 更多