使用C语言实现Linux命令cp

使用C语言实现Linux命令cp

实现拷贝命令,使用方式为cp name.txt  file.txt

//实现文件copy 
#include   
#include  
#include  
#include 
#define MAXSIZE 1024

int main(int argc,char *argv[])
{
	unsigned int start_time=time(NULL);
	if(argc<3)
	{
		printf("参数缺失\n");
		return -1;
	}
	FILE *fp1=fopen(argv[1],"rb");
	FILE *fp2=fopen(argv[2],"wb");
	if(!fp1 || !fp2)
	{
		printf("文件操作失败\n");
		return -2; 
	}
	char *ch=(char *)malloc(MAXSIZE);
	while(!feof(fp1))
	{
		memset(ch,0,MAXSIZE);
		int size=fread(ch,1,MAXSIZE,fp1);
		fwrite(ch,size,1,fp2); 
	}
	fclose(fp1);
	fclose(fp2);
	unsigned int end_time=time(NULL);
	printf("run time=%d\n",end_time-start_time); 
	return 0;
} 

 

你可能感兴趣的:(C,Linux)