C语言利用链表与文件实现登录注册

C语言实现简登录和注册功能

C语言实现注册登录
使用链表
使用文件

版本二:利用链表


此版本使用的链表,第一个版本使用的是数组

数组版本连接

这里我使用的线性链表,一定要注意在判断语句或赋值语句中不可将指针指向未定义的区域,这会产生很大问题,所以一般都需要在链表最后一个节点指向空指针


#代码

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
typedef struct LNode
{
    char name[10];
    char pass[10];
    struct Node *next;
} LNode,*pNode;
//定义节点
pNode createList()
{
    pNode pHead = (pNode)malloc(sizeof(LNode));
    pHead->next=NULL;
    //头结点不存放数据
    FILE *fp = fopen("user.txt","r");
    if(NULL == fp)
    {
        printf("FILE NOT FOUND");
        exit(-1);
    }
    pNode cur = pHead;
    while(1)
    {
        pNode temp = (pNode)malloc(sizeof(LNode));
        if(!temp)
            exit(-1);
            
        //检测到录入完毕后将分配的空间清除掉
        if(2!=fscanf(fp,"%s%s",temp->name,temp->pass))
        {
            free(temp);
            break;
        }
        cur->next=temp;
        cur = temp;

        //使最后一个节点指向空,方便以后判断
        cur->next = NULL;
    }
    return pHead;
}

//登录函数
int login(pNode head)
{
    if(NULL==head->next)
    {
        printf("user list empty\n");
        getch();
        return 0;
    }
    char name[10];
    char pass[10];
    printf("enter your name:");
    scanf("%s",name);
    printf("enter your password:");
    scanf("%s",pass);
    pNode temp = head->next;
    while(temp)
    {
        if(0==strcmp(temp->name,name) && 0==strcmp(temp->pass,pass))
        {
            printf("success");
            getch();
            return 1;
        }
        temp = temp->next;
    }
    printf("user not found");
    getch();
}

//写入txt文件,每一行存在一个用户
void writeToFile(pNode head)
{
    FILE *fw = fopen("user.txt","a+");
    pNode temp=head->next;
    if(temp==NULL){
        return;
    }
    while(temp){
        fprintf(fw,temp->name);
        fprintf(fw,"\t");
        fprintf(fw,temp->pass);
        fprintf(fw,"\n");
        temp  = temp->next;
    }
}


//注册用户
void registerUser(pNode head)
{
    pNode temp = head->next;
    //当表中无用户直接在头结点后注册
    if(!temp)
    {
        temp = (pNode)malloc(sizeof(LNode));
        head->next = temp;
    }
    else
    {
        //表中有用户则在最后一个节点后生成新节点
        while(temp->next)
        {
            temp = temp->next;
        }
        pNode last = (pNode)malloc(sizeof(LNode));
        temp->next = last;
        temp = last;
    }
    printf("enter your name:");
    scanf("%s",temp->name);
    printf("enter your password:");
    scanf("%s",temp->pass);
    temp->next=NULL;
}

int menu()
{
    int choice;
    printf("1.login\n");
    printf("2.register\n");
    printf("#.exit\n");
    printf("enter your choice:");
    scanf("%d",&choice);
    return choice;
}

int main()
{
    int choice;
    pNode head = createList();
    while(1)
    {
        choice = menu();
        if(1==choice)
        {
            system("cls");
            if(login(head))
            {
                //这里写登陆成功的模块
            }
            system("cls");
        }
        else if(2==choice)
        {
            system("cls");
            registerUser(head);
            system("cls");
        }
        else
        {
            writeToFile(head);
            return 0;
        }
    }
}


##运行结果

菜单,比较简陋,可以根据自己需求加东西
这次写了菜单的循环
这里写图片描述

注册
这里写图片描述

登录
这里写图片描述

异常路径(登录失败)
这里写图片描述

posted @ 2018-09-15 08:59  clay_ace  阅读(879)  评论(0编辑  收藏  举报