注册登录
#include "log.h"
void reg()
{char name[20],pwd[20];printf("请输入账号:");fgets(name,sizeof(name),stdin);printf("请输入密码:");fgets(pwd,sizeof(pwd),stdin);//name[strlen(name)-1]='\0';printf("账号密码:%s%s",name,pwd);FILE *fp;fp=fopen("./reg.txt","a");if(fp==NULL){perror("fopen");return;}fprintf(fp,"%s%s",name,pwd);fclose(fp);}
void logi()
{char dname[20],dpwd[20],name[20],pwd[20];printf("请输入登录账号:");fscanf(stdin,"%s",dname);printf("请输入登录密码:");fscanf(stdin,"%s",dpwd);FILE *fp;fp=fopen("./reg.txt","r");if(fp==NULL){perror("fopen");return;}int flag=-1;while(1){int res=fscanf(fp,"%s",name);int res2=fscanf(fp,"%s",pwd);if(res<0)break;if(strcmp(name,dname)==0&&strcmp(pwd,dpwd)==0){flag=0;printf("\n登录成功\n");break;}}if(flag==-1){printf("登录失败\n");}fclose(fp);
}
使用fgets和fputs复制文件
#include <myhead.h>
int main(int argc, const char *argv[])
{FILE *fp1;fp1=fopen("./h1.txt","w");if(fp1==NULL){perror("fopen1");return -1;}FILE *fp2;fp2=fopen("./h2.txt","w");if(fp2==NULL){perror("fopen2");return -1;}fputs("aaa\n",fp1);fputs("bbb\n",fp1);fclose(fp1);fp1=fopen("./h1.txt","r");if(fp1==NULL){perror("fopen1");return -1;}char ch[100];while(1){if(fgets(ch,sizeof(ch),fp1)==NULL)break;printf("ch:%s\n",ch);fputs(ch,fp2);}fclose(fp1);fclose(fp2);return 0;
}
使用fprintf和fscanf实现文件复制
#include <myhead.h>
int main(int argc, const char *argv[])
{FILE *fp1;fp1=fopen("./h1.txt","w");if(fp1==NULL){perror("fopen1");return -1;}FILE *fp2;fp2=fopen("./h2.txt","w");if(fp2==NULL){perror("fopen2");return -1;}fprintf(fp1,"%s\n","ccccc");fprintf(fp1,"%s\n","ddddd");fclose(fp1);fp1=fopen("./h1.txt","r");if(fp1==NULL){perror("fopen1");return -1;}char ch[100];while(1){if(fscanf(fp1,"%s",ch)<0)break;fprintf(fp2,"%s\n",ch);}fclose(fp1);fclose(fp2);return 0;
}