从文件中逐行读取并比较输出/输入

时间:2016-10-21 12:25:23

标签: c

我已经使我的代码正常工作(与工作密切相关),但我遇到了问题。我的注册工作,但登录部分只为第一行工作(读取)...假设我有我的文件(每行的每个帐户):

user1 pass1
user2 pass2
user3 pass3
user4 pass4

只有当我输入用户名为user1和密码为pass1时才会授予我访问权限,但不会为user2和pass2输入密码。我怎样才能解决这个问题 ???代码:

#include <stdio.h>
#include <stdlib.h>
#include "rlutil.h"


void Login();
void Register();
char username[32];
char password[32];
char acc[32];
char pw[32];


int main()
{
    int logreg;
    printf("Press '1' For Login");
    printf("Press '2' For Register\n");
    logreg = getch();
    printf("\n------------------------------------------------\n");
tester:
    ;
    if (logreg == '1')
    {
        Login();
    }
    else if (logreg == '2')
    {
        Register();
    }
    else
    {
        printf("\nInvalid Input!!! Choose between '1' or '2' !!!\n");
        logreg = getch();
        goto tester;
    }
    return 0;
}

void Login()
{
start:
    ;
    char answer;

    // Vnesuvanje na username
    printf("\nEnter your Username: ");
    scanf("%s",username);

    // Vnesuvanje na Password
    printf("\nEnter your Password: ");
    scanf(" %s",password);




    FILE *fData;
    // Otvara file za citanje
    fData = fopen("database.txt", "rt");
    if (!fData)
    {
        printf("The file can not be opened\n\a\a");
    }

    int found=0;

        while(!feof(fData) && !found)
        {
            fscanf(fData, "%s\t%s", acc, pw);
            if (strcmp(username, acc) == 0 && strcmp(password, pw) == 0)
            {
                setColor(LIGHTGREEN);
                printf("\nSuccessfuly logged it to our WebSite\n\n");
                setColor(GREY);
                found = 1;
                getch();
                break;
            }
            else if (!found)
            {
                setColor(LIGHTRED);
                printf("\nNo Access to our WebSite\n\n");
                printf("Invalid username or password!!!\n\n");
                setColor(GREY);
                printf("Would you like to try again?? [y/n] ");
                answer = getch();
                found = 0;
                printf("\n\n------------------------------------------------\n");
                break;
            }
        }

    fclose(fData);

tester2:
    ;
    //proverka za Povtorno pustanje na programata
    if (answer== 'y')
    {
        goto start;
    }
    else
    {
        if (answer!='n')
        {
            printf("Please choose between 'y' or 'n' !!!\n\n");
            answer = getch();
            goto tester2;
        }
        else
        {
            getch();
            return 0;
        }
    }

}

void Register()
{
    char acc[32];
    char pw[32];
    FILE *fData;
    fData = fopen("database.txt", "a");
    if (!fData)
    {
        printf("File could not be opened\n\a\a");
        getchar();
        return;
    }
    printf("Enter your desired Username: ");
    scanf("%s", acc);
    printf("Enter your desired Password: ");
    scanf("%s", pw);
    printf("\n");
    fprintf(fData, "%s\t%s\n", acc, pw);
    fclose(fData);
}

问题应该出在这里:

    FILE *fData;
    // Otvara file za citanje
    fData = fopen("database.txt", "rt");
    if (!fData)
    {
        printf("The file can not be opened\n\a\a");
    }

    int found=0;

        while(!feof(fData) && !found)
        {
            fscanf(fData, "%s\t%s", acc, pw);
            if (strcmp(username, acc) == 0 && strcmp(password, pw) == 0)
            {
                setColor(LIGHTGREEN);
                printf("\nSuccessfuly logged it to our WebSite\n\n");
                setColor(GREY);
                found = 1;
                getch();
                break;
            }
            else if (!found)
            {
                setColor(LIGHTRED);
                printf("\nNo Access to our WebSite\n\n");
                printf("Invalid username or password!!!\n\n");
                setColor(GREY);
                printf("Would you like to try again?? [y/n] ");
                answer = getch();
                found = 0;
                printf("\n\n------------------------------------------------\n");
                break;
            }
        }

    fclose(fData);

但我不知道如何修复代码,如果有人可以修复它并将其粘贴到评论中(答案)我会非常感激。 无论如何,非常感谢你 PS。别介意我,我不擅长编程,但仍愿意学习:)

1 个答案:

答案 0 :(得分:0)

你的逻辑错了。

如果用户名和密码不匹配,则found仍为0,然后将执行else if子句中的语句。

    if (strcmp(username, acc) == 0 && strcmp(password, pw) == 0)
    {
      printf("\nSuccessfuly logged it to our WebSite\n\n");
      found = 1;
      getch();
      break;
    }
    else if (!found)
    {
      printf("\nNo Access to our WebSite\n\n");
      printf("Invalid username or password!!!\n\n");
      printf("Would you like to try again?? [y/n] ");
      answer = getch();
      found = 0;
      printf("\n\n------------------------------------------------\n");
      break;
    }

您需要先扫描整个文件,然后才测试是否找到了用户/密码。

  while (!feof(fData) && !found)
  {
    fscanf(fData, "%s\t%s", acc, pw);
    if (strcmp(username, acc) == 0 && strcmp(password, pw) == 0)
    {
      printf("\nSuccessfuly logged it to our WebSite\n\n");
      found = 1;
      getch();
      break;
    }
  }

  if (!found)
  {
    printf("\nNo Access to our WebSite\n\n");
    printf("Invalid username or password!!!\n\n");
    printf("Would you like to try again?? [y/n] ");
    answer = getch();
    found = 0;
    printf("\n\n------------------------------------------------\n");
  }

BTW:与您的问题没有直接关系: 请阅读this SO article