【问题标题】:Reading line by line from file and compare the output/input从文件中逐行读取并比较输出/输入
【发布时间】:2016-10-21 12:25:22
【问题描述】:

我已经让我的代码正常工作(接近正常工作),但我遇到了问题。我的注册工作,但登录部分仅适用于第一行...假设我有这样的文件(每行的每个帐户):

user1 pass1
user2 pass2
用户3 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。别介意我,我不擅长编程但仍然愿意学习:)

【问题讨论】:

  • 是时候学习如何使用调试器了。

标签: c


【解决方案1】:

你的逻辑错了。

如果用户名和密码不匹配,则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");
  }

顺便说一句:与您的问题没有直接关系: 请阅读this SO article

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-20
    • 2020-04-08
    • 1970-01-01
    • 1970-01-01
    • 2014-01-07
    • 1970-01-01
    • 2011-05-18
    相关资源
    最近更新 更多