C语言实现的文件拷贝

#include <stdio.h>	
#include <stdlib.h>
#include <string.h>
//#include <pthread.h>	

char buf[4097];
int flag = 0;
int file_size = 0;
FILE * source = NULL;
FILE * target = NULL;

//pthread_mutex_t thread_lock;

FILE * client_openfile()
{
	
	if(source == NULL)
	{
		printf("文件打开失败!\n");
		exit(-1);
	}

	fseek(source,0,SEEK_END);
	file_size = ftell(source);
	fseek(source,0,SEEK_SET);

	printf("文件大小为:%d\n",file_size);

	return source;	
}

FILE * server_openfile()
{

	if(target == NULL)
	{
		printf("文件打开失败!\n");
		exit(-1);
	}

	return target;	
}

void read_write()
{
	int size;
	FILE *fp1 = NULL;
	FILE *fp2 = NULL;

	if(flag == 0)
	{
		fp1 = client_openfile();
		fp2 = server_openfile();
		flag = 1;
	}

	memset(buf,0,sizeof(buf));

	while((size = fread(buf,1,4096,fp1)) != 0)
		fwrite(buf,1,size,fp2);
	//	pthread_mutex_unlock(&thread_lock);
}

int main(int argc,char *argv[])
{
	/*	pthread_t thread0,thread1;
		pthread_mutex_init(&thread_lock,NULL);

		while(1)
		{
		pthread_mutex_lock(&thread_lock);
		pthread_create(&thread0,NULL,client_readfile,NULL);
		pthread_mutex_trylock(&thread_lock);
		pthread_create(&thread1,NULL,server_writefile,NULL);
		}

		pthread_mutex_destroy(&thread_lock);*/

	source = fopen(argv[1],"rb");
	target = fopen(argv[2],"wb+");

	read_write();

	return 0;	
}

编译出a.out可执行程序以后

./a.out  文件源路径  目标文件路径


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值