本文目錄一覽:
如何用C語言編程實現用戶登錄
C語言的話,一般用戶信息存儲在結構體鏈表裡
你輸入用戶名回車以後,需要遍歷鏈表,使用strcmp()函數逐一對比鏈表裡是否存儲了你輸入的用戶名。不存在輸出“無此用戶”,存在繼續輸入密碼,將密碼與此結點的密碼信息對比,處理方式同用戶名;
至少三次輸入錯誤,可設一個整形變量index = 0,每錯誤一次執行index++,當if(index==3)成立時,輸出相應信息,並執行exit(1);
多用戶登錄系統C語言程序
#include stdio.h
#include stdlib.h
#include “string.h”
#include “windows.h”
int total=0;
struct u_p
{
char user[20];
char pass[20];
} s[50];
void read()
{
total=GetPrivateProfileInt(“INFO”,”count”,0,”d:\\Info.dat”);
int i;
char t[5]={“\0”};
for(i=0;itotal;i++)
{
sprintf(t,”%d”,i+1);
GetPrivateProfileString(t,”USER”,””,s[i].user,20,”d:\\Info.dat”);
GetPrivateProfileString(t,”PASSWORD”,””,s[i].pass,20,”d:\\Info.dat”);
}
}
void input()
{
int p,i=0,count=0,f_u=0,f_p=0;
char user[20]={“\0”};
char password[20]={“\0”};
while(1)
{
f_u=0;
f_p=0;
system(“cls”);
printf(“當前共有%d個註冊用戶”,total);
printf(“\n\n請輸入用戶名:”);
memset(user,’\0′,20);
scanf(“%s”,user);
printf(“\n請輸入密碼:”);
memset(password,’\0′,20);
i=0;
while(1)
{
p=_getch();
if(p==10 || p==13)
{
break;
}
password[i++]=p;
printf(“*”);
}
for(i=0;itotal;i++)
{
if(strcmp(s[i].user,user)==0)
{
f_u=1;
if(strcmp(s[i].pass,password)==0)
{
f_p=1;
printf(“\n\n歡迎 %s”,user);
fflush(stdin);
_getche();
continue;
}
}
}
if(f_u==0)
{
printf(“\n\n不存在該用戶名! 選 1 重新輸入,選 2 註冊新用戶”);
int c=0;
fflush(stdin);
c=_getche();
if(c==’1′)
{
continue;
}
else if(c==’2′)
{
system(“cls”);
printf(“註冊新用戶”);
printf(“\n\n\n請輸入用戶名:”);
memset(user,’\0′,20);
scanf(“%s”,user);
printf(“\n請輸入密碼:”);
char temp[20]={“\0”} ;
i=0;
while(1)
{
p=_getch();
if(p==10 || p==13)
{
break;
}
temp[i++]=p;
printf(“*”);
}
printf(“\n請再次輸入密碼:”);
i=0;
memset(password,’\0′,20);
while(1)
{
p=_getch();
if(p==10 || p==13)
{
break;
}
password[i++]=p;
printf(“*”);
}
if(strcmp(temp,password)==0)
{
total++;
char t[5]={“\0”};
sprintf(t,”%d”,total);
WritePrivateProfileString(“INFO”,”count”,t,”d:\\Info.dat”);
WritePrivateProfileString(t,”USER”,user,”d:\\Info.dat”);
WritePrivateProfileString(t,”PASSWORD”,password,”d:\\Info.dat”);
printf(“\n\n註冊成功,請重新登錄”);
fflush(stdin);
_getch();
count=0;
read();
continue;
}
else
{
printf(“\n\n兩次密碼不一致,註冊失敗”);
fflush(stdin);
_getch();
count=0;
continue;
}
}
}
else if(f_p==0)
{
count++;
if(count=3)
{
printf(“\n\n連續輸入3次錯誤,程序將退出”);
fflush(stdin);
_getche();
return ;
}
printf(“\n\n密碼輸入錯誤,請重新輸入”);
fflush(stdin);
_getche();
}
}
return ;
}
int main(int argc, char *argv[])
{
read();
input();
return 0;
}
C語言用戶登錄系統賬戶密碼比對
#include stdio.h
#include string.h
typedef struct account{
char name[32];
char acc[16];
char psw[16];
}Acc;
// data是結構體數組,filename是文件絕對地址,n保存讀入的結構體數量
void GetDataFromTxt(Acc* data, const char* filename, int* n)
{
FILE *fp = fopen(filename, “r”);
if( NULL == fp ){
printf(“Open file failed or no this file!\n”);
return;
}
int i = 0;
while( !feof(fp) )
{
fscanf(fp, “%s %s %s”, data[i].name, data[i].acc, data[i].psw);
i++;
}
*n = i;
}
int main()
{
int i, n;
Acc data[100];
// 獲取數據
GetDataFromTxt(data, “E:\\secret.txt”, n);
printf(“n = %d\n”, n);
printf(“姓名 賬號 密碼\n”);
for(i = 0; i n; ++i)
printf(“%-4s %-16s %-10s\n”, data[i].name, data[i].acc, data[i].psw);
// 登錄示例
putchar(‘\n’);
char acc[16], psw[16];
do{
// 這裡只是粗略地寫了一個
// 具體的賬號錯誤或者密碼錯誤自行發揮
printf(“請輸入賬號:”);
scanf(“%s”, acc);
printf(“請輸入密碼:”);
scanf(“%s”, psw);
for(i = 0; i n; ++i)
{
if( strcmp(acc,data[i].acc)==0 strcmp(psw,data[i].psw)==0 ){
printf(“登陸成功!\n”);
break;
}
}
if( i == n ){
printf(“賬號或密碼不正確!請重新輸入!\n\n”);
}else{
break;
}
}while(1);
printf(“Bye bye!!!\n”);
return 0;
}
C語言編寫一個用戶登陸的程序?
代碼如下:
#includestdio.h
#pragma warning(disable:4996)
#includestring.h
int main()
{
int i = 0;
char password[10] = { 0 };
printf(“請輸入密碼:”);
while (i 3)
{
scanf(“%s”, password);
printf(“\n”);
if (strcmp(password, “972816”) == 0)
{
printf(“登錄成功\n”);
break;
}
else
{
i++;
if (i != 3)
printf(“再輸入一次”);
}
}
if (i == 3)
printf(“密碼錯誤三次退出登錄界面\n”);
system(“pause”);
return 0;
擴展資料:
#include後面有兩種方式,;和””前者先在標準庫中查找,查找不到在path中查找。後者為文件路徑,若直接是文件名則在項目根目錄下查找。
引用方法:#include stdio.h
注意事項:在TC2.0中,允許不引用此頭文件而直接調用其中的函數,但這種做法是不標準的。也不建議這樣做。以避免出現在其他IDE中無法編譯或執行的問題。
參考資料來源:百度百科—include
參考資料來源:百度百科—stdio.h
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/252153.html