reg.c
#include "reg.h"
int regist()
{
stu s;
printf("请输入注册账号:");
fgets(s.username,sizeof(s.username),stdin);
printf("请输入注册密码:");
fgets(s.password,sizeof(s.password),stdin);
FILE *fp = fopen("./1.txt","w");
if(fp == NULL)
{
perror("fopen");
return -1;
}
fprintf(fp,"%s%s",s.username,s.password);
printf("注册成功!\n");
fclose(fp);
return 0;
}
main.c
#include "reg.h"
int main (int argc, const char *argv[])
{
int ch;
while(1)
{
printf("\t\t\t1.注册\n");
printf("\t\t\t2.登录\n");
printf("\t\t\t3.退出\n");
printf("请输入你的选择:");
scanf("%d",&ch);
getchar();
switch(ch)
{
case 1:
regist();
break;
case 2:
login();
break;
case 3:
exit(EXIT_SUCCESS);
break;
}
}
return 0;
}
login.c
#include "reg.h"
int login()
{
stu s;
printf("请输入用户账号:");
fgets(s.username,sizeof(s.username),stdin);
printf("请输入用户密码:");
fgets(s.password,sizeof(s.password),stdin);
s.username[strlen(s.username)-1] = '\0';
s.password[strlen(s.password)-1] = '\0';
stu t;
FILE *fp = fopen("./1.txt","r");
if(fp == NULL)
{
perror("fopen");
return -1;
}
fscanf(fp,"%s%s",t.username,t.password);
if(strcmp(s.username,t.username) == 0
&& strcmp(s.password,t.password) == 0 )
{
printf("登录成功!\n");
}else
{
printf("密码或者账户错误!\n");
}
return 0;
}
使用fwrite和fread复制文件
#include <myhead.h>
int main(int argc, const char *argv[])
{
if(argc != 3)
{
printf("外部参数错误!\n");
return -1;
}
FILE *fp1 = fopen("./1.txt","r");
if(fp1 == NULL)
{
perror("fp1");
return -1;
}
FILE *fp2 = fopen("./2.txt","w");
if(fp2 == NULL)
{
perror("fp2");
return -1;
}
char s[100];
fread(s,sizeof(s),1,fp1);
fwrite(s,sizeof(s),1,fp2);
printf("复制成功!\n");
fclose(fp1);
fclose(fp2);
printf("复制后2.txt的内容:\n");
fp2 = fopen("./2.txt","r");
char r[100];
fread(r,sizeof(r),1,fp2);
printf("%s\n",r);
fclose(fp2);
return 0;
}