Search code examples
cfunctiondata-structureslinked-listfile-handling

C - Login using file handling and linked list


I am creating an account using Create New Account option, & then writing the created username pwd to the file, but when I rerun the code & try to login using the same id, it fails. Using string compare, but it seems string is not being read.

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);
        }

EDIT:
The value of pUser->username is not what it should be

Can anyone tell me what I'm doing wrong. Does this have anything to do with the fact that I'm rerunning the code so maybe the pointers are reallocated and not pointing to where they should? In that case, how do I store the pointers so that they aren't changed from the first run.


Solution

  • This isn't strictly a linked list as the structures are not read into memory. For this, the *next pointer isn't used. Each structure is read from the file and the strcmp comparisons check for equality.
    For adding structures the file is opened using "a+" to append to the file.
    If either attempt at opening the file fails, it tries again using "w+" and if that fails, the program exits.
    Unsure what accessUser() was supposed to do, so it is commented out.
    A function could be added to read all the structures into memory and link them using the *next pointer.

    #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);
    }