使用fread和fwrite拷贝文件。
#include<stdio.h>
#include<string.h>
#include<myhead.h>
int main(int argc, const char *argv[])
{
/*int num =10000;
FILE *fp=fopen("./1.txt","w+");
fwrite(&num,sizeof(int),1,fp);
fclose(fp);*/
if(argc!=3)
{
printf("外部参数错误\n");
return -1;
}
FILE *fp1=fopen("./1.txt","r");
FILE *fp2=fopen("./2.txt","w");
char buff[100];
char p[100];
while(1)
{
if(fread(p,1,1,fp1)!=0)
{
fwrite(p,1,1,fp2);
printf("成功\n");
}
else
{
//fwrite(p,1,1,fp2);
break;
}
}
fclose(fp1);
fclose(fp2);
return 0;
}
作业1:使
#include"log.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("输入你的选择:\n");
scanf("%d",&ch);
getchar();
switch(ch)
{
case 1:
regist();break;
case 2:
login();break;
case 3:
exit(EXIT_SUCCESS);break;
}
}
return 0;
}
用分文件编译,实现注册登录界面,使用已经学过的fgets,fscanf,fpritnf函数。
#include"log.h"
int login()
{
user log,reg;
printf("请输入账户:");
fgets(log.name,sizeof(log.name),stdin);
printf("输入登录密码");
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",reg.name);
//reg.name[strlen(reg.name)]='\n';
//reg.code[strlen(reg.code)]='\n';
fscanf(fp,"%s",reg.code);
//reg.code[strlen(reg.code)]='\n';
if(strcmp(log.name,reg.name)==0&&
strcmp(log.code,reg.code)==0)
{
printf("登陆成功\n");
}
}
#include"log.h"
int regist()
{
user reg;
printf("输入注册的账户:");
fgets(reg.name,sizeof(reg.name),stdin);
printf("请输入密码:");
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;
}
#ifndef _LOG_H_
#define _LOG_H_
#include<myhead.h>
typedef struct
{
char name[200];
char code[200];
}user;
int regist();
int login();
#endif