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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

Vecace   2021-01-28 我要评论

C语言实现简登录和注册功能,供大家参考,具体内容如下

C语言实现注册登录

使用链表
使用文件

版本二: 利用链表

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

数组版本连接

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

代码:

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
typedef struct LNode
{
 char name[10];
 char pass[10];
 struct LNode *next;
} LNode,*pNode;
//定义节点
pNode createList()
{
 //此函数是为了构造一个链表,存储用户信息用的。
 
 //链表头结点声明,这个头结点也就是链表的入口,你可以通过头结点,找到链表所有的数据
 pNode pHead = (pNode)malloc(sizeof(LNode));
 pHead->next=NULL;
 //头结点不存放数据
 
 //以读的方式打开user.txt文件
 FILE *fp = fopen("user.txt","r+");
 if(NULL == fp)
 {
 //如果没有该文件,则退出并显示未找到文件
  printf("FILE NOT FOUND");
  exit(-1);
 }
 
 //获取链表入口,也就是头结点
 pNode cur = pHead;
 while(1)
 {
 //从user.text循环读入所有数据并依次存于链表
 
 //先制造一个临时节点
  pNode temp = (pNode)malloc(sizeof(LNode));
  if(!temp)
   exit(-1);
   
 //下面fscanf是从文件读取信息,并存入节点的name变量和pass变量
  //如果fscanf的返回值不是2,则说明后面没有数据了,也即是录入完毕
 //检测到录入完毕后将分配的空间清除掉
  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");
  getchar();
  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");
   getchar();
   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;
  }
 }
}

运行结果

菜单,比较简陋,可以根据自己需求加东西

这次写了菜单的循环

注册

登录

异常路径(登录失败)

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们