2024.7.26 作业
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;
}
思维导图
原文地址:https://blog.csdn.net/qq_64434282/article/details/140722697
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!