C語言利用鏈表與文件實現(xiàn)登錄注冊功能
C語言實現(xiàn)簡登錄和注冊功能,供大家參考,具體內(nèi)容如下
C語言實現(xiàn)注冊登錄
使用鏈表
使用文件
版本二: 利用鏈表
此版本使用的鏈表,第一個版本使用的是數(shù)組
這里我使用的線性鏈表,一定要注意在判斷語句或賦值語句中不可將指針指向未定義的區(qū)域,這會產(chǎn)生很大問題,所以一般都需要在鏈表最后一個節(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()
{
//此函數(shù)是為了構(gòu)造一個鏈表,存儲用戶信息用的。
//鏈表頭結(jié)點聲明,這個頭結(jié)點也就是鏈表的入口,你可以通過頭結(jié)點,找到鏈表所有的數(shù)據(jù)
pNode pHead = (pNode)malloc(sizeof(LNode));
pHead->next=NULL;
//頭結(jié)點不存放數(shù)據(jù)
//以讀的方式打開user.txt文件
FILE *fp = fopen("user.txt","r+");
if(NULL == fp)
{
//如果沒有該文件,則退出并顯示未找到文件
printf("FILE NOT FOUND");
exit(-1);
}
//獲取鏈表入口,也就是頭結(jié)點
pNode cur = pHead;
while(1)
{
//從user.text循環(huán)讀入所有數(shù)據(jù)并依次存于鏈表
//先制造一個臨時節(jié)點
pNode temp = (pNode)malloc(sizeof(LNode));
if(!temp)
exit(-1);
//下面fscanf是從文件讀取信息,并存入節(jié)點的name變量和pass變量
//如果fscanf的返回值不是2,則說明后面沒有數(shù)據(jù)了,也即是錄入完畢
//檢測到錄入完畢后將分配的空間清除掉
if(2!=fscanf(fp,"%s%s",temp->name,temp->pass))
{
free(temp);
break;
}
//讓當(dāng)前節(jié)點(鏈表的尾部)的后面加上讀取到數(shù)據(jù)的節(jié)點
cur->next=temp;
//讓當(dāng)前的節(jié)點為鏈表尾部
cur = temp;
//使最后一個節(jié)點指向空,方便以后判斷
cur->next = NULL;
}
return pHead;
}
//登錄函數(shù)
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;
//當(dāng)表中無用戶直接在頭結(jié)點后注冊
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;
}
}
}
運行結(jié)果
菜單,比較簡陋,可以根據(jù)自己需求加?xùn)|西
這次寫了菜單的循環(huán)

注冊

登錄

異常路徑(登錄失?。?/p>

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
對比C語言中的setbuf()函數(shù)和setvbuf()函數(shù)的使用
這篇文章主要介紹了對比C語言中的setbuf()函數(shù)和setvbuf()函數(shù)的使用,涉及到緩沖區(qū)與流的相關(guān)知識,需要的朋友可以參考下2015-08-08
舉例剖析C++中引用的本質(zhì)及引用作函數(shù)參數(shù)的使用
這篇文章主要介紹了C++中引用的本質(zhì)及引用作函數(shù)參數(shù)的使用,講解了函數(shù)返回值是引用的情況等一些難點,需要的朋友可以參考下2016-03-03
C++實現(xiàn)一個線程安全的單例工廠實現(xiàn)代碼
這篇文章主要介紹了 C++實現(xiàn)一個線程安全的單例工廠實現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2017-05-05
淺談int8_t int64_t size_t ssize_t的相關(guān)問題(詳解)
下面小編就為大家?guī)硪黄獪\談int8_t int64_t size_t ssize_t的相關(guān)問題(詳解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-03-03
C語言 structural body結(jié)構(gòu)體詳解用法
C 數(shù)組允許定義可存儲相同類型數(shù)據(jù)項的變量,結(jié)構(gòu)是 C 編程中另一種用戶自定義的可用的數(shù)據(jù)類型,它允許您存儲不同類型的數(shù)據(jù)項,結(jié)構(gòu)用于表示一條記錄,假設(shè)您想要跟蹤圖書館中書本的動態(tài),您可能需要跟蹤每本書的下列屬性2021-10-10

