copy.c实现

#cat copy.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int copyFile(char *fileRead,char *fileWrite);

int main(){
	char fileRead[100];
	char fileWrite[100];

	printf("要复制的文件:");
	scanf("%s",fileRead);
	printf("目标文件:");
	scanf("%s",fileWrite);

	if(copyFile(fileRead,fileWrite)){
		printf("拷贝成功");
	}else{
		printf("复制失败");
	}

	return 0;
}

int copyFile(char *fileRead,char *fileWrite){
	FILE *fpRead;
	FILE *fpWrite;
	int bufferLen=1024*4;
	char *buffer = (char*)malloc(bufferLen);
	int readCount;

	if((fpRead=fopen(fileRead,"rb")) == NULL || (fpWrite=fopen(fileWrite,"wb")) == NULL){
		printf("can not open file");
		exit(1);
	}

	while((readCount=fread(buffer,1,bufferLen,fpRead)) > 0){
		fwrite(buffer,readCount,1,fpWrite);
	}

	free(buffer);
	fclose(fpRead);
	fclose(fpWrite);
	return 1;

}

posted @ 2017-01-23 15:22  苏小北1024  阅读(242)  评论(0编辑  收藏  举报