C語言利用鏈表與文件實(shí)現(xiàn)登錄注冊(cè)功能
C語言實(shí)現(xiàn)簡登錄和注冊(cè)功能,供大家參考,具體內(nèi)容如下
C語言實(shí)現(xiàn)注冊(cè)登錄
使用鏈表
使用文件
版本二: 利用鏈表
此版本使用的鏈表,第一個(gè)版本使用的是數(shù)組
這里我使用的線性鏈表,一定要注意在判斷語句或賦值語句中不可將指針指向未定義的區(qū)域,這會(huì)產(chǎn)生很大問題,所以一般都需要在鏈表最后一個(gè)節(jié)點(diǎn)指向空指針
代碼:
#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é)點(diǎn) pNode createList() { //此函數(shù)是為了構(gòu)造一個(gè)鏈表,存儲(chǔ)用戶信息用的。 //鏈表頭結(jié)點(diǎn)聲明,這個(gè)頭結(jié)點(diǎn)也就是鏈表的入口,你可以通過頭結(jié)點(diǎn),找到鏈表所有的數(shù)據(jù) pNode pHead = (pNode)malloc(sizeof(LNode)); pHead->next=NULL; //頭結(jié)點(diǎn)不存放數(shù)據(jù) //以讀的方式打開user.txt文件 FILE *fp = fopen("user.txt","r+"); if(NULL == fp) { //如果沒有該文件,則退出并顯示未找到文件 printf("FILE NOT FOUND"); exit(-1); } //獲取鏈表入口,也就是頭結(jié)點(diǎn) pNode cur = pHead; while(1) { //從user.text循環(huán)讀入所有數(shù)據(jù)并依次存于鏈表 //先制造一個(gè)臨時(shí)節(jié)點(diǎn) pNode temp = (pNode)malloc(sizeof(LNode)); if(!temp) exit(-1); //下面fscanf是從文件讀取信息,并存入節(jié)點(diǎn)的name變量和pass變量 //如果fscanf的返回值不是2,則說明后面沒有數(shù)據(jù)了,也即是錄入完畢 //檢測(cè)到錄入完畢后將分配的空間清除掉 if(2!=fscanf(fp,"%s%s",temp->name,temp->pass)) { free(temp); break; } //讓當(dāng)前節(jié)點(diǎn)(鏈表的尾部)的后面加上讀取到數(shù)據(jù)的節(jié)點(diǎn) cur->next=temp; //讓當(dāng)前的節(jié)點(diǎn)為鏈表尾部 cur = temp; //使最后一個(gè)節(jié)點(diǎn)指向空,方便以后判斷 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文件,每一行存在一個(gè)用戶 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; } } //注冊(cè)用戶 void registerUser(pNode head) { pNode temp = head->next; //當(dāng)表中無用戶直接在頭結(jié)點(diǎn)后注冊(cè) if(!temp) { temp = (pNode)malloc(sizeof(LNode)); head->next = temp; } else { //表中有用戶則在最后一個(gè)節(jié)點(diǎn)后生成新節(jié)點(diǎn) 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; } } }
運(yùn)行結(jié)果
菜單,比較簡陋,可以根據(jù)自己需求加?xùn)|西
這次寫了菜單的循環(huán)
注冊(cè)
登錄
異常路徑(登錄失?。?/p>
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C++ 虛函數(shù)和純虛函數(shù)的區(qū)別分析
這篇文章主要介紹了C++ 虛函數(shù)和純虛函數(shù)的區(qū)別,幫助大家更好的理解和學(xué)習(xí)c++的相關(guān)知識(shí),感興趣的朋友可以了解下2020-10-10C++實(shí)現(xiàn)歌手比賽評(píng)分系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)歌手比賽評(píng)分系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03QT實(shí)現(xiàn)二、八、十六進(jìn)制之間的轉(zhuǎn)換
本文主要介紹了QT實(shí)現(xiàn)二、八、十六進(jìn)制之間的轉(zhuǎn)換,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05C語言實(shí)現(xiàn)食堂就餐管理系統(tǒng)(帶鏈表)
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)食堂就餐管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11C語言使用strcmp()函數(shù)比較兩個(gè)字符串的實(shí)現(xiàn)
這篇文章主要介紹了C語言使用strcmp()函數(shù)比較兩個(gè)字符串的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01C++實(shí)現(xiàn)基于控制臺(tái)界面的吃豆子游戲
這篇文章主要介紹了C++實(shí)現(xiàn)基于控制臺(tái)界面的吃豆子游戲,實(shí)例分析了吃豆子游戲的原理與C++實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-04-04