在简单文件登录程序中注册时出错

问题描述 投票:-2回答:2

我之前已经问过,并且已经做了一些我认为会工作的东西(但我是初学者,以便故事落入水中)我有一个错误,当我试图注册时,任何人都可以帮忙吗?我有更多的时间发送它(家庭作业)我不要求别人为我这样做,只是为了给我一些指示

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

int signIn(char username[30], char pass[30]){

FILE *p;
char user2[30], pass2[30];

p = fopen("users.txt", "r+");


fscanf(p,"%s\n%s",user2,pass2);

if( (strcmp(username,user2)==0) && (strcmp(pass,pass2)==0) )
    printf("\nUser and password correct!!!");
else
    printf("\nUser or password incorrect!\n\n");

printf("\n\n");

fclose(p);
return 0;
}

int signUp(char username[30], char pass[30], char fullName[30]) {

FILE *p;
p = fopen("users.txt", "r+");

printf("Username: ");
scanf("%s", &username);

printf("Password: ");
scanf("%s", &pass);

printf("Full name: ");
scanf("%s", &fullName);

fprintf(p, "%s\n%s\n%s", username, fullName, pass);

fclose(p);
return 0;
}

int main(){
char username[30], pass[30], fullName[30];
int choose;

printf("Welcome to student login system!\n");
printf(" 1: Sign in\n 2: Sign up\n");
printf("--------------------------------\n");
scanf("%d", &choose);

if(choose==1) {
    printf("\nUser:");
    scanf("%s",username);
    printf("\nPassword:");
    scanf("%s",pass);
    signIn(username, pass);
}

if(choose==2) {

    signUp(username, pass, fullName);

}

}

我尝试了很多不同的方式,现在我真的很困惑..

c
2个回答
1
投票
  1. scanf("%s", &username);不正确。它应该是scanf("%s", &username[0]);应该集中注意警告,特别是涉及的数组/指针。
  2. 同样低于2行 scanf("%s", &pass[0]); scanf("%s", &fullName[0]);
  3. fprintf(p, "%s\n%s\n%s", username, fullName, pass);您将全名写为第二行,但在阅读时,您将第二行读作密码。所以fprintf(p, "%s\n%s\n%s", username, pass,fullName );
  4. 另一个建议是,在打开文件后,应检查文件指针的有效性。由于某种原因,如果它返回NULL,那么你的程序会崩溃。

调试这个简单代码的最佳方法不是简单地尝试编译和获取o / p,而是用笔和纸坐下来,尝试分析每行发生的情况


0
投票

你的代码中出现了许多错误的东西。我试着尽可能多地修理。以下应该有效。

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

int signIn(char username[], char pass[]) {
    FILE *p;
    char user2[30], pass2[30], fullName[50];

    if (!(p = fopen("users.txt", "r+"))) {
        printf("Could not read from users.txt file\n");
        fclose(p);
        return 1;
    }

    int ret = 2;
    while (fgets(user2, 30, p) != NULL) {
        fgets(pass2, 30, p);
        fgets(fullName, 50, p);
        // you have written the fullName to the file as a third line, so you must check for this too
        if ((strcmp(username, user2) == 0) && (strcmp(pass, pass2) == 0)) {
            printf("\nUser and password correct!!!\n");
            printf("Logged in as %s", fullName);
            ret = 0;
            break;
        }
    }

    if (ret == 2)
        printf("\nUser or password incorrect!\n");

    printf("\n\n");
    fclose(p);
    return ret;
}

int signUp(void) {
    FILE *p;
    char username[30], pass[30], fullName[50];

    if (!(p = fopen("users.txt", "w+"))) {
        printf("Could not write to users.txt file\n");
        fclose(p);
        return 1;
    }

    printf("Username: ");
    fgets(username, 30, stdin);

    printf("Password: ");
    fgets(pass, 30, stdin);

    printf("Full name: ");
    fgets(fullName, 50, stdin);

    fprintf(p, "%s%s%s", username, pass, fullName);
    printf("You have signed up!\n");
    fclose(p);
    return 0;
}

int main(void) {
    char username[30], pass[30];
    int choose;

    printf("Welcome to student login system!\n");
    printf(" 1: Sign in\n 2: Sign up\n");
    printf("--------------------------------\n");
    scanf("%d", &choose);
    getchar();

    if (choose == 1) {
        printf("User: ");
        fgets(username, 30, stdin);
        printf("Password: ");
        fgets(pass, 30, stdin);
        return signIn(username, pass);
    }
    else if (choose == 2) {
        return signUp();
    }
    return 0;
}

编辑:我为邋answer的答案道歉,我不在我的主电脑上。此代码适用于阅读1个用户。

© www.soinside.com 2019 - 2024. All rights reserved.