1、使用fgets统计给定文件行数
代码:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main(int argc, const char *argv[])
{if(argc != 2){printf("inputs file error\n");printf("usage:./a.out filename\n");return -1;}//定义文件指针,以只读形式打开文件FILE *fp = NULL;if((fp = fopen(argv[1],"r")) == NULL){perror("fopen error");return -1;}int line=0;char buf[5]="";while(1){char *ptr=fgets(buf,sizeof(buf),fp);if(ptr==NULL){break;}while(*ptr!='\0'){if(*ptr=='\n'){line++;}ptr++;}}printf("line=%d\n",line);//关闭文件fclose(fp);return 0;
}
运行结果:
2、使用fputs和fgets完成两个文件的拷贝
代码:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main(int argc, const char *argv[])
{if(argc != 3){printf("inputs file error\n");printf("usage:./a.out srcfile destfile\n");return -1;}//定义文件指针,以只读形式打开文件FILE *srcfp = NULL;if((srcfp = fopen(argv[1],"r")) == NULL){perror("fopen error");return -1;}//定义文件指针,以只写形式打开文件FILE *destfp = NULL;if((destfp = fopen(argv[2],"w")) == NULL){perror("fopen error");return -1;}char buf[10]="";while(1){char *ptr=fgets(buf,sizeof(buf),srcfp);if(ptr==NULL){break;}fputs(ptr,destfp);}//关闭文件fclose(srcfp);fclose(destfp);return 0;
}
运行结果:
3、完成注册登录功能
做个小菜单,功能1是注册功能,输入注册账户和注册密码,将账户和密码写入文件中
功能2是登录功能,提示并输入登录账户和登录密码,并用其遍历文件中的每一组账户和密码,如果匹配成功,则登录成功,如果全部不匹配则提示登录失败
代码:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main(int argc, const char *argv[])
{int menu=0;while(1){// system("clear"); //执行终端指令printf("************xx登录界面*********************\n");printf("**************1->注册**********************\n");printf("**************2->登录**********************\n");printf("**************3->退出**********************\n");printf("请输入选项:");scanf("%d",&menu);getchar();//菜单多分支选择switch(menu){case 1:{//注册功能//存储账号FILE *accountfp=NULL;if((accountfp=fopen("./account.txt","a"))==NULL){perror("fopen error");return -1;}//定义要写入的用户名账号并存储char account[10]="";printf("please enter account:");fscanf(stdin,"%s",account);getchar();fputs(account,accountfp);// fputc('\n',accountfp);fclose(accountfp);//存储密码FILE *passwdfp=NULL;if((passwdfp=fopen("./passwd.txt","a"))==NULL){perror("fopen error");return -1;}//定义要写入的用户名密码并存储char passwd[10]="";printf("please enter passwd:");fscanf(stdin,"%s",passwd);getchar();fputs(passwd,passwdfp);// fputc('\n',passwdfp);fclose(passwdfp);// system("clear"); //执行终端指令}break;case 2:{//登录功能char buf[10]="";//验证账户char account[10]="";FILE *accountfp=NULL;if((accountfp=fopen("./account.txt","r"))==NULL){perror("fopen error");return -1;}printf("please enter account:");fscanf(stdin,"%s",account);while(1){//遍历文件中的账户比较char *ptr=fgets(buf,sizeof(buf),accountfp);if(strcmp(buf,account)==0){printf("account true!\n");break;}if(ptr==NULL){printf("account error\n");goto AERROR;break;}}fclose(accountfp);//验证密码char passwd[10]="";FILE *passwdfp=NULL;if((passwdfp=fopen("./passwd.txt","r"))==NULL){perror("fopen error");return -1;}printf("please enter passwd:");fscanf(stdin,"%s",passwd);while(1){//遍历文件中的账户比较char *ptr=fgets(buf,sizeof(buf),passwdfp);if(strcmp(buf,passwd)==0){printf("passwd true!\n");printf("登录成功!\n");break;}if(ptr==NULL){printf("passwd error\n");printf("登录失败\n");break;}}fclose(passwdfp);}break;case 0:goto END;default:printf("error,please try again\n");}//提示输入任意键按回车结束printf("请输入任意键按回车结束!!!\n");while(getchar()!='\n');}
AERROR:printf("登录失败\n");
END:return 0;
}
运行结果: