作业1:使用分文件编译,实现注册登录界面
int main(int argc, const char *argv[])
{
while(1)
{
int key;
printf("1.注册\n");
printf("2.登录\n");
printf("3.退出\n");
printf("请选择功能:");
scanf("%d",&key);
getchar();
switch(key)
{
case 1:regist();
break;
case 2:login();
break;
case 3:return 0;
}
}
return 0;
}
#include "log.h"
int regist()
{
FILE*fp = fopen("./3.txt","w");
if(fp == NULL)
{
perror("fp");
return -1;
}
list st;
printf("请输入您要注册的账号:");
fgets(st.name,sizeof(st.name),stdin);
printf("请输入您要注册的密码:");
fgets(st.code,sizeof(st.code),stdin);
fprintf(fp,"%s",st.name);
fprintf(fp,"%s",st.code);
fclose(fp);
return 0;
}
int login()
{
list st,reg;
printf("请输入您的账号:");
fgets(reg.name,sizeof(reg.name),stdin);
printf("请输入您的密码:");
fgets(reg.code,sizeof(reg.code),stdin);
FILE*fp = fopen("./3.txt","r");
if(fp == NULL)
{
perror("fp");
return -1;
}
fscanf(fp,"%s",st.name);
fscanf(fp,"%s",st.code);
strcat(st.name,"\n");
strcat(st.code,"\n");
if(strcmp(reg.name,st.name)==0&&strcmp(reg.code,st.code)==0)
{
printf("登录成功\n");
}
else
{
printf("登录失败\n");
}
fclose(fp);
}
#ifndef _LOG_H_
#define _LOG_H_
#include <myhead.h>
typedef struct
{
char name[20];
char code[20];
}list;
int regist();
int login();
#endif
作业2:使用fread和fwrite拷贝文件
#include <myhead.h>
int main(int argc, const char *argv[])
{
FILE*fp1 = fopen("./1.txt","r");
FILE*fp2 = fopen("./2.txt","w");
if(fp1 == NULL || fp2 == NULL)
{
perror("fopen");
return -1;
}
char arr[100];
int a;
while((a = fread(arr,1,sizeof(arr),fp1))>0)
{
fwrite(arr,a,1,fp2);
}
fclose(fp1);
fclose(fp2);
return 0;
}