C語言利用鏈表與文件實現登錄注冊功能
更新時間:2020年12月28日 09:53:44 作者:Vecace
這篇文章主要介紹了C語言利用鏈表與文件實現登錄注冊功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
C語言實現簡登錄和注冊功能,供大家參考,具體內容如下
C語言實現注冊登錄
使用鏈表
使用文件
版本二: 利用鏈表
此版本使用的鏈表,第一個版本使用的是數組
這里我使用的線性鏈表,一定要注意在判斷語句或賦值語句中不可將指針指向未定義的區(qū)域,這會產生很大問題,所以一般都需要在鏈表最后一個節(jié)點指向空指針
代碼:
#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;
//定義節(jié)點
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循環(huán)讀入所有數據并依次存于鏈表
//先制造一個臨時節(jié)點
pNode temp = (pNode)malloc(sizeof(LNode));
if(!temp)
exit(-1);
//下面fscanf是從文件讀取信息,并存入節(jié)點的name變量和pass變量
//如果fscanf的返回值不是2,則說明后面沒有數據了,也即是錄入完畢
//檢測到錄入完畢后將分配的空間清除掉
if(2!=fscanf(fp,"%s%s",temp->name,temp->pass))
{
free(temp);
break;
}
//讓當前節(jié)點(鏈表的尾部)的后面加上讀取到數據的節(jié)點
cur->next=temp;
//讓當前的節(jié)點為鏈表尾部
cur = temp;
//使最后一個節(jié)點指向空,方便以后判斷
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
{
//表中有用戶則在最后一個節(jié)點后生成新節(jié)點
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;
}
}
}
運行結果
菜單,比較簡陋,可以根據自己需求加東西
這次寫了菜單的循環(huán)

注冊

登錄

異常路徑(登錄失敗)

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
對比C語言中的setbuf()函數和setvbuf()函數的使用
這篇文章主要介紹了對比C語言中的setbuf()函數和setvbuf()函數的使用,涉及到緩沖區(qū)與流的相關知識,需要的朋友可以參考下2015-08-08
淺談int8_t int64_t size_t ssize_t的相關問題(詳解)
下面小編就為大家?guī)硪黄獪\談int8_t int64_t size_t ssize_t的相關問題(詳解)。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-03-03

