【问题标题】:Password reader String Array密码阅读器字符串数组
【发布时间】:2014-04-16 03:52:10
【问题描述】:

因此,我的程序旨在为用户提供出色的服务并要求输入密码。一旦用户输入密码,它将与我的预编码密码“ans[]”进行比较,如果密码与用户输入的密码匹配,则打印欢迎问候语。

#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main(int argc, const char * argv[])
{
char hello[] = "Hello! \nPassword Required:";
char password[50];
char ans[] = "Zanderg!";

printf(hello);
printf("\n");
gets(password);

if (strcmp(password, ans) != 0) {
    do {
    printf("%s Not correct.\n", password);
    printf("Enter Password:\n");
    gets(password);
    getchar();
    }while (strcmp(password, ans) != 0);
};

if (strcmp(password, ans) == 0) {
    printf("welcome %s", password);
}
}

我的问题是,当我输入正确的密码时,我仍然收到“密码错误”的消息。

我也收到了一条关于gets() 不安全的非常奇怪的消息,我想知道是否有其他gets() 的替代方法,或者如何在我的程序中消除此错误消息。我的编译器是 Xcode。

【问题讨论】:

  • 你的程序在我的系统(gcc)上运行。您使用的是哪个编译器?
  • 是的,它曾经工作过一次,但自从......我已经改变了任何东西,所以我不确定问题是什么。我正在使用 Xcode @SGG
  • 没关系...我发现了问题。 @SGG
  • +if 语句后加分号
  • @Zanderg 有什么问题?

标签: c strcmp gets do-loops


【解决方案1】:

Zanderg 说他找到了问题所在,但他从不费心提及问题所在。不管任何可能仍然感兴趣的人,我都会继续发布它。

让我们试试下面这段代码:

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>

int main(int argc, const char * argv[])
{
    char password[50];
    char ans[] = "Zanderg!";

    printf("Hello! \nPassword Required:\n");
    fgets(password, sizeof(ans),stdin);

    if (strcmp(password, ans) != 0) {
        do {
        printf("%s Not correct.\n", password);
        printf("Enter Password:\n");
        fgets(password, sizeof(ans),stdin);
        getchar();
        }while (strcmp(password, ans) != 0);
    };

    if (strcmp(password, ans) == 0) {
        printf("welcome %s", password);
    }
}

另外,是的,gets 已被弃用。像上面一样使用fgets

编辑(回答评论问题):

  1. 按照说明书:

    char* gets(char *s):gets() reads a line from stdin into the buffer pointed to by s until either a terminating newline or EOF, which it replaces with a null byte ('\0'). No check for buffer overrun is performed. Never use gets(). Because it is impossible to tell without knowing the data in advance how many characters gets() will read, and because gets() will continue to store characters past the end of the buffer, it is extremely dangerous to use. It has been used to break computer security. Use fgets() instead.

    char *fgets(char *s, int size, FILE *stream):fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A terminating null byte ('\0') is stored after the last character in the buffer.

  2. 我指的是sizeof(ans),因为strcmp 将继续比较直到遇到空字符。因此,在password 内部,我们只想写到ans 的大小,然后用空字符填充结尾。您还可以做的是将其更改为使用strncmp,它可以比较高达n 字节。在这种情况下,您不必告诉fgets 最多读取sizeof(ans) 字节。

【讨论】:

  • 好的,你回答了我真正的问题,这就是 gets() 和 fgets() 之间的主要区别是什么导致了我在使用 gets() 时得到的错误语句? @wyas
  • 是的,我输入密码的问题在于 ans[]= Zanderg!。我有那个感叹号,我没有意识到它在那里。 @wyas
  • 还有一个问题要问你......这就是为什么当你使用 fgets(password) 时你指的是 ans 的大小? @wyas
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-19
  • 1970-01-01
  • 1970-01-01
  • 2018-02-06
  • 2019-04-24
相关资源
最近更新 更多