第一题
#include <myhead.h>
int main(int argc, const char *argv[])
{
FILE *fp = fopen("./1.txt","r");
if(fp==NULL)
{
perror("fopen");
return -1;
}
char buf[100]={0};
int sum =0;
while(fgets(buf,sizeof(buf),fp)!=NULL)
{
if(buf[strlen(buf)]=='\0')
{
sum++;
}
}
printf("%d\n",sum);
fclose(fp);
return 0;
}
第二题
#include <myhead.h>
int main(int argc, const char *argv[])
{
if(argc!=3)
{
printf("外部传参错误\n");
return -1;
}
FILE *fp1 = fopen("./1.txt","r");
if(NULL==fp1)
{
perror("fp1");
return -1;
}
FILE *fp2 = fopen("2.txt","w");
if(NULL==fp2)
{
perror("fp2");
return -1;
}
char buf[100]={0};
while(fgets(buf,sizeof(buf),fp1)!=NULL)
{
fputs(buf,fp2);
}
fclose(fp1);
fclose(fp2);
printf("拷贝成功\n");
#if 1
fp2 = fopen("2.txt","r");
if(NULL==fp2)
{
perror("fp2");
return -1;
}
char buf2[100]={0};
while(fgets(buf2,sizeof(buf2),fp2))
{
fputs(buf2,stdout);
}
#endif
return 0;
}