作业:fgets和fputs拷贝文件。
#include <myhead.h>
int main(int argc, const char *argv[])
{
if(argc!=3)
{
printf("外部参数错误\n");
return -1;
}
FILE *fp1=fopen("./1.txt","r");
if(fp1==NULL)
{
perror("fp1");
return -1;
}
FILE *fp2=fopen("./2.txt","w");
if(fp2==NULL)
{
perror("fp2");
return -1;
}
char buff[100];
while(fgets(buff,sizeof(buff),fp1))
{
fputs(buff,fp2);
}
fclose(fp1);
fclose(fp2);
printf("拷贝成功\n");
fp2=fopen("./2.txt","r");
if(NULL==fp2)
{
perror("fp2");
return -1;
}
char rev[100];
while(fgets(rev,sizeof(rev),fp2))
{
fputs(rev,stdout);
}
fclose(fp2);
return 0;
}
作业:fgets获取文件的行号。
#include <myhead.h>
int main(int argc, const char *argv[])
{
FILE *fp=fopen("./2.txt","r");
if(fp==NULL)
{
perror("fp");
return -1;
}
char buff[5];
int count=0;
while(fgets(buff,sizeof(buff),fp))
{
if(buff[strlen(buff)-1]=='\n')
{
count++;
}
}
printf("行号有%d行\n",count);
return 0;
}
作业:Xmind总结。