作业1 fgets获取文件的行号
#include <myhead.h>
int main(int argc, const char *argv[])
{
FILE *pf=fopen("./1.txt","r");
if(pf==NULL)
{
perror("fp");
return -1;
}
char buff[20];
int count=0;
int len;
while(fgets(buff,sizeof(buff),pf))//输出完为NULL,循环自动结束
{
len=strlen(buff)-1;
if(buff[len]=='\n')
{
count++;
}
}
printf("行数为%d\n",count);
return 0;
}
作业2 fgets和fputs拷贝文件
#include <myhead.h>
int main(int argc, const char *argv[])
{
if(argc!=3)
{
printf("外部参数错误\n");
return -1;
}
FILE *pf1=fopen("./1.txt","r");
if(pf1==NULL)
{
perror("pf1");
return -1;
}
FILE *pf2=fopen("./2.txt","w");
if(pf2==NULL)
{
perror("f2");
return -1;
}
char bu1[20];
while(fgets(bu1,sizeof(bu1),pf1))//输出完为NULL,循环自动结束
{
fputs(bu1,pf2);//拷贝给pf2
}
fclose(pf1);
fclose(pf2);
printf("拷贝成功\n");
//查看文件2,看是否拷贝成功
pf2=fopen("./2.txt","r");
char bu2[20];
while(fgets(bu2,sizeof(bu2),pf2))//循环输出文件2
{
fputs(bu2,stdout);
}
fclose(pf2);
return 0;
}
作业3 思维导图