程序
#include<stdio.h>
#include<string.h>
void copy_file(FILE *src,FILE *dest)
{
int ch = 0;
while(1)
{
ch=fgetc(src);
if(ch==EOF)
break;
fputc(ch,dest);
}
}
int get_line(FILE *dest)
{
int line = 0;
char buf[1024]={0};
char *ch;
while(1)
{
ch=fgets(buf,sizeof(buf),dest);
if(ch == NULL )
break;
if(buf[strlen(buf)-1]=='\n')
line++;
}
return line;
}
int main(int argc, const char *argv[])
{
FILE *src_file;
FILE *dest_file;
int lineNum = 0;
if(argc != 3)
{
fprintf(stderr,"usage:%s src_file!",argv[0]);
return -1;
}
//源文件,只读
src_file = fopen(argv[1],"r");
if(src_file == NULL)
{
perror("文件打开失败");
return -1;
}
//目标文件,只写
dest_file = fopen(argv[2],"w");
if(dest_file == NULL)
{
perror("文件打开失败");
return -1;
}
//复制文件
copy_file(src_file,dest_file);
fclose(src_file);
fclose(dest_file);
//输出行数
dest_file = fopen(argv[2],"r");
if(dest_file == NULL)
{
perror("文件打开失败");
return -1;
}
lineNum=get_line(dest_file);
printf("行数为:%d 文件为%s\n",lineNum,argv[2]);
fclose(dest_file);
return 0;
}
结果