1.使用fgets统计给定文件的行号
#include <myhead.h>
int main(int argc,const char *argv[])
{if(argc!=2){printf("input file error!!!\n");printf("usage:./a.out fileName\n");return -1;}FILE *fp=NULL; if( (fp=fopen(argv[1],"r"))==NULL ){printf("fopen error\n");return -1;}char buf[100]="";int count=0;while(1){memset(buf,0,sizeof(buf));if(fgets(buf,sizeof(buf),fp)==NULL){break;}count++;}printf("一共有%d行\n",count);fclose(fp);return 0;
}
2.使用fgets、fputs完成两个文件的拷贝
#include <myhead.h>
int main(int argc,const char *argv[])
{if(argc!=3){printf("input file error!!!\n");printf("usage:./a.out fileName1 fileName2\n");return -1;}FILE *fp1=NULL; if( (fp1=fopen(argv[1],"r"))==NULL ){printf("fopen error\n");return -1;}FILE *fp2=NULL; if( (fp2=fopen(argv[2],"w"))==NULL ){printf("fopen error\n");return -1;}char buf[100]="";while(1){memset(buf,0,sizeof(buf));if(fgets(buf,sizeof(buf),fp1)==NULL){break;}fputs(buf,fp2);}return 0;
}
思维导图