#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(int argc,const char *argv[])
{
if(argc<3){
printf("请输入要拷贝的文件名\n");
return -1;
}
FILE *fp1=fopen(argv[1],"r");
if(NULL == fp1){
perror("fopen");
return -1;
}
FILE *fp2=fopen(argv[2],"w");
if(NULL == fp2){
perror("fopen");
return -1;
}
int count=0;
int num=0;
char c;
while(1){
c = fgetc(fp1);
if(c<0)
break;
if(c=='\n')
num++;
count++;
fputc(c,fp2);
}
printf("文件大小:%d\n",count);
printf("文件行数:%d\n",num-1);
fclose(fp1);
fclose(fp2);
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct{
char id[20];
char password[20];
}people;
int main(int argc,const char *argv[])
{
people p = {0};
char name[20],key[20];
printf("input id>");
scanf("%s",name);
printf("input password>");
scanf("%s",key);
//打开存储账户密码的文件
FILE * fp = fopen("./usr.txt","r");
if(NULL == fp){
perror("fopen");
return -1;
}
int count=0,num=0;
//读取文件内信息
while(1){
int num1 = fscanf(fp,"%s",p.id);
if(num1 < 0)
break;
int num2 = fscanf(fp,"%s",p.password);
if(num2 < 0)
break;
//对账户名进行比较
if(strcmp(p.id,name)==0){
count++;//存在账户,则计数器num加一
//对密码进行比较
if(strcmp(p.password,key)==0){
num++;//存在密码,则计数器num加一
printf("登录成功\n");
break;//成功登录则直接退出循环
}
}
bzero(p.id,0);
bzero(p.password,0);
}
if(count==0)
printf("账户不存在\n");
else if(num==0)
printf("密码错误\n");
fclose(fp);
return 0;
}