1.fgets获取文件的行号。
#include <stdio.h>
#include <string.h>
#include <myhead.h>
int main(int argc,const char*argv[])
{
FILE *fp = fopen("./1.txt","r");打开文件
if(fp == NULL)
{
perror("fp");
return -1;
}
char buff[100];
int count = 0;
int i=0;
while(1)
{
printf("%s\n",buff);
if(fgets(buff,sizeof(buff),fp)==NULL)判断是否结束读取
{
break;
}
else if(buff[strlen(buff)-1]=='\n')
{
count++;
}
}
printf("%d\n",count);
fclose(fp);
return 0;
}
2.fgets和fputs拷贝文件。
#include <stdio.h>
#include <string.h>
#include <myhead.h>
int main(int argc,const char*argv[])
{
if(argc!=3)
{
printf("外部参数错误");
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("fp1");
return -1;
}
char buff[100];
while(1)
{
if(fgets(buff,sizeof(buff),fp1)==NULL)判断第一个文件是否结束
{
break;
}
else
{
fputs(buff,fp2);拷贝
}
}
fclose(fp1);
fclose(fp2);
return 0;
}
3.思维导图