在 C 中重复输入数据的结构

Structures repeating entered data in C

我正在为 "C" 编程语言 class 开发一个项目。我有一个问题,我一直无法找到补救措施。问题出在我正在使用的结构中。它接受我输入的任何数据并重复 6 次。我的密码是

对于结构:

    struct authstruct
    {
        char userName[MAXCHAR];
        char userPin[MAXCHAR];
    };

正在接收输入的行

    struct authstruct usr;
    printf("Please enter your username\n");
    scanf("%s",usr.userName);
    while (fscanf(fident, "%s%s", auth.userName, auth.userPin) != EOF)
    {
        printf("%s", usr.userName);
    };

请注意,我将其设置为打印它收到的用于测试目的的代码。例如,如果您输入

test

它会踢出去

testtesttesttesttesttest

整个代码(忽略不相关的功能,没有上面的编辑)是:

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

/* Defining of constants */
#define MAXCHAR 10
#define MAXATTEMPTS 3
#define SIZE 10

/* Prototypes */
int AuthFunc(void);

/* Prototype of structure for Authentication Function */
 struct authstruct
{    
    char userName[MAXCHAR];
    char userPin[MAXCHAR];
};

int main(VOID)
{
    int login;
    login = AuthFunc();
    if (login == -1)
    {
        printf("namepass.txt could not be found\nPlease check for file and try again\n");
    }
    else if (login == 1)
    {
        printf("You have tried too many times to login\nPlease restart and try again\n");
    }
    else if (login == 0)
    {
        printf("Success!");
    }
    return 0;
}

/* This function authenticates the user */
int AuthFunc(void)
{
    int i = 0;
    FILE *fident;
    fident=fopen("namepass.txt", "r");
    if (fident==NULL)
    {
        return -2;
    }

    printf("Hello! Please follow my instructions\n");
    struct  authstruct auth;
    for (i=0; i < MAXATTEMPTS; i++)
    {
        struct authstruct usr;
        printf("Please enter your username\n");
        scanf("%s",usr.userName);
        while (fscanf(fident, "%s%s", auth.userName, auth.userPin) != EOF)
        {
            if ((strcmp(usr.userName, auth.userPin)) == 0)
            {
                printf ("Please enter your password.\n");
                scanf ("%s",usr.userPin);
                if ((strcmp(usr.userPin, auth.userPin)) == 0)
                {
                    fclose(fident);
                    return 0;
                }
            }
        }
        printf("Incorrect username/password.\n");
    }    
    fclose(fident);
    return 1;
}

为了 运行,您需要创建一个名为 namepass.txt 的新 .txt 文件,并将您选择的用户名/密码排列在一行中,如下例

test    12345
test2    23456

我为冗长的 post 道歉,我只是想尽可能地描述性。这是在 Xcode 7.3 中 运行 在 Mac 运行ning OSX 10.11.4 上完成的。如果您的机器上没有出现此问题,请告诉我,我将开始研究我的编译器设置。 谢谢

while (fscanf(fident, "%s%s", auth.userName, auth.userPin) != EOF) 看起来不对。 fscanf returns 一个 int 如果两个读数都通过,则将是 2

修改为:

while (fscanf(fident, "%s%s", auth.userName, auth.userPin) == 2)

此外,如 WhozCraig in 所述,您应该将文件倒回到开头或将文件的打开和关闭限制在 AuthFunc.

中的 for 循环内

来自 man fscanf:

These functions return the number of input items successfully matched and assigned, which can be fewer than provided for, or even zero in the event of an early matching failure.

The value EOF is returned if the end of input is reached before either the first successful conversion or a matching failure occurs. EOF is also returned if a read error occurs, in which case the error indicator for the stream (see ferror(3)) is set, and errno is set indicate the error.