1.
#includeint main(int argc, const char *argv[]){FILE *fp=NULL;if( (fp=fopen("./01text.txt","w"))==NULL ){perror("fopen error");return -1;}fputs("hello\n",fp);fputs("world\n",fp);fclose(fp);fp=NULL;if( (fp=fopen("./01text.txt","r"))==NULL ){perror("fopen error");return -1;}char buf[5]="";int count=0;while(1){char *ptr =fgets(buf,sizeof(buf),fp);if(ptr==NULL)break;for(int i=0;i<5;i++){if( *(ptr+i)=='\n')count++;}}printf("有%d行\n",count);fclose(fp);return 0;}
2.
#includeint main(int argc, const char *argv[]){if(argc !=3 ){perror("ERROR");return -1;}FILE *srcfp=NULL;if( (srcfp=fopen(argv[1],"a"))==NULL ){perror("fopen error");return -1;}FILE *destfp=NULL;if( (destfp=fopen(argv[2],"r"))==NULL ){perror("fopen error");return -1;}char ch[5]="";while(1){char *ptr=fgets(ch,sizeof(ch),destfp);if(ptr==NULL)break;fputs(ch,srcfp);}fclose(srcfp);fclose(destfp);return 0;}
3.
#include#includeint main(int argc, const char *argv[]){int menu = 0;while(1){system("clear");//执行终端指令printf("\t\t======登录界面============\n");printf("\t\t======1、注册============\n");printf("\t\t======2、登录============\n");printf("\t\t======0、退出============\n");printf("请输入选项:");scanf("%d", &menu);getchar();//对菜单多分支选择switch(menu){case 1:{//注册功能FILE *reg_fp=NULL;if( (reg_fp=fopen("./zhuche.c","w"))==NULL){perror("fopen error");return -1;}printf("please enter reg_acc");char a[20]="";char *p1=fgets(a,sizeof(a),stdin);fputs(p1,reg_fp);printf("please enter reg_pass");char b[20]="";char *p2=fgets(b,sizeof(b),stdin);fputs(p2,reg_fp);fclose(reg_fp);}break;case 2:{//登录功能FILE *log_fp=NULL;if( (log_fp=fopen("./denglu.c","w"))==NULL){perror("fopen error");return -1;}printf("please enter log_acc");char c[20]="";char *p3=fgets(c,sizeof(c),stdin);fputs(p3,log_fp);printf("please enter log_pass");char d[20]="";char *p4=fgets(d,sizeof(d),stdin);fputs(p4,log_fp);fclose(log_fp);}break;case 0:goto END;default:printf("您输入的功能有误,请重新输入\n");}//提示请输入回车键结束printf("请输入回车键结束!!!\n");while(getchar() != '\n'); }END:FILE *reg_fp=NULL;if( (reg_fp=fopen("./zhuche.c","r"))==NULL){perror("fopen error");return -1;}FILE *log_fp=NULL;if( (log_fp=fopen("./denglu.c","r"))==NULL){perror("fopen error");return -1;}char a[20]="";int i=0;char *p=fgets(a,sizeof(a),reg_fp);char *q=fgets(a,sizeof(a),log_fp);while(p!=NULL && q!=NULL){if(p[i]==q[i])printf("登录成功\n");elseprintf("登录失败\n");i++;}fclose(reg_fp);fclose(log_fp);return 0;}