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

#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;
}
}
展开阅读全文

没有更多推荐了,返回首页