头文件定义结构体
#include <stdio.h>
#include<stdlib.h>
#include<string.h>
struct stu{
char psw[10];
};
接着让用户输入一个密码 保存在psw中
int main()
{
struct stu a;
printf("enter you password\n");
gets(a.psw);
if(check(&a))
printf("log in");
else
printf("wrong");
}
接着封装一个check函数
int check(struct stu *a)
{
int flag=0;//定义一个开关开判断密码的accessbility
FILE *fp;//文件指针
char user1[10],user2[10],psw[10];
//user1 来保存用户输入的密码
//user2 来从文件读取密码
if((fp=fopen("/Users/xiebaba/Downloads/user-2.txt","r+"))==NULL)//打开文件
{
printf("failed");
exit(0);
}
strcpy(user1,a->psw);//调用函数strcpy
*注意此函数传入的是指针 得用->*
while(!feof(fp))
{
fgets(user2,10,fp);
//fgets函数 (array,number,pointer)
if(strcmp(user1,user2)==0)
{
flag=1;
}
}
return flag;
}
运行结果