fgets读取行数
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
//判断文件个数是否为2
if(argc != 2)
{
printf("input file error\n");
printf("usage:./a.out fileName\n");
return -1;
}
//定义文件指针
FILE *fp;
if ((fp = fopen(argv[1],"r")) == NULL)
{
perror("fopen error");
return -1;
}
int count = 0;
char s[2000];
while (fgets(s,sizeof(s),fp) != NULL)
{
count++;
}
fclose(fp);
printf("count = %d\n",count);
return 0;
}
fgets和fputs实现拷贝
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
//定义文件指针
FILE *srcfp;
FILE *destfp;
//以只读形式打开文件
if((srcfp = fopen("./01open.c","r")) == NULL)
{
perror("fopen error");
return -1;
}
//以只写形式打开文件
if((destfp = fopen("./dest.txt","w")) == NULL)
{
perror("fopen error");
return -1;
}
//定义一个容器
char s[200];
//循环读取数据
while(fgets(s,sizeof(s),srcfp) != NULL)
{
//读取的数据放在第二个文件中
fputs(s,destfp);
}
//关闭文件
fclose(srcfp);
fclose(destfp);
return 0;
}