C语言 关于用户名与密码的存放(文件)

来源:互联网 发布:sql中join on的用法 编辑:程序博客网 时间:2024/05/21 06:13
#include <stdio.h>
#include <stdlib.h>


struct user
{
char username[20];
char password[20];
}use;
 

void main ()
{
FILE *fp;
char c;


while(1)
{
if ((fp = fopen("测试.txt","rb+")) == NULL)
{
if ((fp = fopen("测试.txt","wb+")) == NULL)
printf("can not open this file");

printf("用户名:");scanf("%s",use.username);
printf("密码:");scanf("%s",use.password);


if (fwrite(&use,sizeof(struct user),1,fp) != 1)
printf("file write error");
fclose(fp);


}
else
{
if((fp = fopen("测试.txt","a")) == NULL)
printf("can not open this file");

printf("用户名:");scanf("%s",use.username);
printf("密码:");scanf("%s",use.password);
fwrite(&use,sizeof(struct user),1,fp);

}
fclose(fp);
fflush(stdin);
printf("是否继续注册(Y/N)?\t");
scanf("%c",&c);
if (c == 'N'||c == 'n')break;
}
}
0 0
原创粉丝点击