int main(int argc, const char *argv[])
{
FILE *Q = fopen("1.txt", "r");
if (Q == NULL)
{
perror("打开源文件失败");
return -1;
}
FILE *T = fopen("2.txt", "w");
if (T == NULL)
{
perror("打开目标文件失败");
return -1;
}
// 读取并写入文件
char buff[30];
fread(buff,1,sizeof(buff),Q);
fwrite(buff, 1, sizeof(buff),T);
// 检查是否有错误发生
if (ferror(Q))
{
perror("读取源文件时发生错误");
fclose(Q);
fclose(T);
return -1;
}
// 关闭文件
fclose(Q);
fclose(T);
printf("文件拷贝完成。\n");
return 0;
}
#include "text.h"
int zhuce()
{
user log;
printf("注册的账户:");
fgets(log.name,sizeof(log.name),stdin);
printf("注册的密码:");
fgets(log.code,sizeof(log.code),stdin);
FILE *Q=fopen("./1.txt","w");
if(Q==NULL)
{
perror("fopen");
return -1;
}
fprintf(Q,"%s%s",log.name,log.code);
printf("注册成功!\n");
fclose(Q);
return 0;
}
int denglu()
{
user log,leg;
printf("请输入账号:");
fgets(log.name,sizeof(log.name),stdin);
printf("请输入密码:");
fgets(log.code,sizeof(log.code),stdin);
FILE *Q=fopen("./1.txt","r");
if(Q==NULL)
{
perror("fopen");
return -1;
}
fscanf(Q,"%s %s",leg.name,leg.code);
strcat(leg.name,"\n");
strcat(leg.code,"\n");
fclose(Q);
if(strcmp(log.name,leg.name)==0
&&strcmp(log.code,leg.code)==0)
{
printf("成功\n");
}
else
{
printf("失败\n");
}
}
![](https://i-blog.csdnimg.cn/direct/9f89785d4f3449cdb0d229c8d128ea30.png)