1> 使用fgets统计一个文件的行号
#include<myhead.h>
int main(int argc, const char *argv[])
{
FILE* fp=NULL;
if((fp = fopen("./1.c","r"))==NULL)
{
perror("fopen");
return -1;
}
char arr[100]="";
int count=0;
while(1)
{
char* str=fgets(arr,sizeof(arr),fp);
if(str==NULL)
break;
int len=strlen(arr);
if(arr[len-1]=='\n')
count++;
}
printf("行号为%d\n",count);
fclose(fp);
return 0;
}
2> 使用fgets\fputs拷贝两个文件
#include<myhead.h>
int main(int argc, const char *argv[])
{
FILE* fp=NULL;
FILE* fq=NULL;
if((fp = fopen("./fgets.c","r")) == NULL)
{
perror("fopen");
return -1;
}
if((fq = fopen("./2.c","w")) == NULL)
{
perror("fopen");
return -1;
}
char arr[100]="";
while(1)
{
char* str=fgets(arr,sizeof(arr),fp);
if(str==NULL)
break;
fputs(str,fq);
}
fclose(fp);
fclose(fq);
return 0;
}