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

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

C语言实现注册登录

使用链表

使用文件

版本二: 利用链表

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

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

代码:

#include

#include

#include

#include

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;

}

}

}

运行结果

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

这次写了菜单的循环

659ff21b51548e11289b4f77c62902f5.png

注册

b7295fea273744fe84671eb0188cada5.png

登录

7f6f5041967e87f50e7f61478cea3bf8.png

异常路径(登录失败)

a1c3ec3d039efc5ab14f43b296cf48ee.png

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值