注册登录界面
main.c
#include "log.h"
int main(int argc, const char *argv[])
{
int ch;
while(1)
{
printf("\t\t\t\n1.注册");
printf("\t\t\t\n2.登录");
printf("\t\t\t\n3.退出");
printf("\n请输入你的选择:");
scanf("%d",&ch);
getchar();
switch(ch)
{
case 1:
regist();
break;
case 2:
login();
break;
case 3:
exit(EXIT_SUCCESS);
break;
}
}
return 0;
}
reist.c
#include "log.h"
int regist()
{
user reg;
printf("请输入注册的账户:\n");
fgets(reg.name,sizeof(reg.name),stdin);
printf("请输入注册的密码:\n");
fgets(reg.code,sizeof(reg.code),stdin);
FILE *fp= fopen("./user.txt","w");
if(fp==NULL)
{
perror("fopen");
return -1;
}
fprintf(fp,"%s %s",reg.name,reg.code);
printf("注册成功\n");
fclose(fp);
return 0;
}
login.c
#include "log.h"
int login()
{
user log,reg;
printf("请输入登录账户:\n");
fgets(log.name,sizeof(log.name),stdin);
printf("请输入登录密码:\n");
fgets(log.code,sizeof(log.code),stdin);
log.name[strlen(log.name)-1]='\0';
log.code[strlen(log.code)-1]='\0';
FILE *fp = fopen("./user.txt","r");
if(fp==NULL)
{
perror("fopen");
return -1;
}
fscanf(fp,"%s%s",reg.name,reg.code);
if(strcmp(log.name,reg.name)==0&&
strcmp(log.code,reg.code)==0)
{
printf("登陆成功");
}
else
{
printf("失败");
}
return 0;
}
log.h
#ifndef _LOG_H_
#define _LOG_H_
#include <myhead.h>
typedef struct
{
char name[20];
char code[20];
}user;
int login();
int regist();
#endif
fwrite和fread实现复制
ubuntu@ubuntu:day2$
ubuntu@ubuntu:day2$ cat 1.c
#include <myhead.h>
int main(int argc, const char *argv[])
{
int a=1000;
int num;
FILE *fp1=fopen("./1.txt","w+");
if(fp1==NULL)
{
perror("fopen");
return -1;
}
fwrite(&a,sizeof(int),1,fp1);
fclose(fp1);//写入1000
fp1=fopen("./1.txt","r");
fread(&num,1,sizeof(int),fp1);
printf("num=%d\n",num);
fclose(fp1);//读取1000
FILE *fp2=fopen("./2.txt","w+");
fwrite(&num,1,sizeof(int),fp2);
printf("num=%d\n",num);
fclose(fp2);//赋值1000
return 0;
}