C - 使用文件处理和链接列表登录

问题描述 投票:0回答:4

我正在使用“创建新帐户”选项创建一个帐户,然后将创建的用户名 pwd 写入文件,但是当我重新运行代码并尝试使用相同的 ID 登录时,它失败了。使用字符串比较,但似乎没有读取字符串。

struct user{
    char username[10];
    char password[10];
    struct user *next;
}*sUser,*pUser; 

userlogin(void){
FILE *fp;
char uName[10], pwd[10];int i;char c;
sUser=pUser=(struct user *)malloc(sizeof(struct user));

printf("1. Login Through An Existing Account\n2. Create New account\n");
        scanf("%d",& i);
        system("cls");
        switch(i){
            case 1:
                fp=fopen("user.dat", "w");
                printf("Username: ");
                scanf("%s",&uName);
                printf("Password: ");
                scanf("%s",&pwd);
                fread (pUser, sizeof(struct user), 1, fp);
                while(pUser!=NULL){
                    if(pUser->username==uName){
                        if(pUser->password==pwd){
                            accessUser();
                        }
                    }
                    pUser=pUser->next;
                }
                break;

            case 2: 
                do
                {
                    fp=fopen("user.dat", "r");
                    printf("Choose A Username: ");
                    scanf("%s",&pUser->username);
                    printf("Choose A Password: ");
                    scanf("%s",&pUser->password);
                    printf("Add another account? (Y/N): ");
                    fflush(stdin);
                    scanf("%c",&c);
                    if(c=='Y'||c=='y'){
                        pUser->next=(struct user*)malloc(sizeof(struct user));
                    }
                }while(c=='Y'||c=='y');
                pUser->next==NULL;
                fwrite (sUser, sizeof(struct user), 1, fp);
                break;
            }
            fclose(fp);
        }

编辑:
pUser->username 的值不是它应该的值

谁能告诉我我做错了什么。 这与我重新运行代码所以指针可能被重新分配而不是指向它们应该指向的位置这一事实有什么关系吗? 在这种情况下,我如何存储指针,以便它们从第一次运行开始就不会改变。

c function data-structures linked-list file-handling
4个回答
1
投票

替换这个:

printf("Username: ");
scanf("%s",&uName);
printf("Password: ");
scanf("%s",&pwd);

与:

printf("Username: ");
scanf("%s", uName);
printf("Password: ");
scanf("%s", pwd);

同:

printf("Choose A Username: ");
scanf("%s",&pUser->username);
printf("Choose A Password: ");
scanf("%s",&pUser->password);

(删除&符号

&
)。这是因为对于 char 数组,变量名被视为指针。

字符串比较也不正确。替换:

if(pUser->username==uName)
if(pUser->password==pwd)

与:

if ( strcmp(pUser->username, uName) == 0 )
if ( strcmp(pUser->password, pwd) == 0 )

0
投票

这并不是严格意义上的链表,因为结构不会读入内存。 为此,不使用 *next 指针。从文件中读取每个结构,并通过 strcmp 比较检查是否相等。
要添加结构,请使用“a+”打开文件以附加到文件。
如果打开文件的任何尝试失败,它将使用“w+”再次尝试,如果失败,程序将退出。
不确定

accessUser()
应该做什么,所以被注释掉了。
可以添加一个函数来将所有结构读入内存并使用 *next 指针链接它们。

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

void userlogin(void);

struct user{
    char username[10];
    char password[10];
}*pUser;

int main()
{
    userlogin ( );

    return 0;
}

void userlogin(void){
    FILE *fp;
    char uName[10], pwd[10];int i;char c;

    pUser=(struct user *)malloc(sizeof(struct user));

    printf("1. Login Through An Existing Account\n2. Create New account\n");
    scanf("%d",& i);
    //system("cls");
    switch(i){
        case 1:
            if ( ( fp=fopen("user.dat", "r+")) == NULL) {
                if ( ( fp=fopen("user.dat", "w+")) == NULL) {
                    printf ("Could not open file\n");
                    exit ( 1);
                }
            }
            printf("Username: ");
            scanf("%9s",uName);
            printf("Password: ");
            scanf("%9s",pwd);
            while ( fread (pUser, sizeof(struct user), 1, fp) == 1) {
                if( strcmp ( pUser->username, uName) == 0) {
                    printf ("Match username\n");
                    if( strcmp ( pUser->password, pwd) == 0) {
                        printf ("Match password\n");
                        //accessUser();
                    }
                }
            }
            break;

        case 2:
            do
            {
                if ( ( fp=fopen("user.dat", "a+")) == NULL) {
                    if ( ( fp=fopen("user.dat", "w+")) == NULL) {
                        printf ("Could not open file\n");
                        exit ( 1);
                    }
                }
                printf("Choose A Username: ");
                scanf("%9s",pUser->username);
                printf("Choose A Password: ");
                scanf("%9s",pUser->password);
                fwrite (pUser, sizeof(struct user), 1, fp);
                printf("Add another account? (Y/N): ");
                scanf(" %c",&c);//skip leading whitespace
            }while(c=='Y'||c=='y');
            break;
    }
    free ( pUser);//free allocated memory
    fclose(fp);
}

0
投票

字符串比较不适用于 C 中的

==
。请改用
strcmp


0
投票

错误:
在情况 1 中,您以写入模式打开文件,但在这种情况下读取数据。类似地,第二种情况文件以读取模式打开,但在这种情况下写入数据

更正: 在第一种情况下以读取模式打开文件,在第二种情况下以写入模式打开文件。

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