MFC 实现文件的Copy功能

const int BUFF_SIZE = 10240 + 256 + 4 + 4 ;

void CtestDlg::My_ReadFile(const char *filepath)
{
	if (NULL == filepath)
	{
		MessageBox("参数文件名字为空");
		return;
	}
	
	ifstream input_file_stream;
	input_file_stream.open(filepath, std::ios::binary);// 以输入流打开文件
	if (!input_file_stream)
	{
		MessageBox("打开文件失败!");
		return;
	}
	input_file_stream.seekg(0, ios::end);//将文件指针移动至末尾
	int d_file_length = input_file_stream.tellg();// 获取文件长度
	input_file_stream.seekg(0, ios::beg);//将文件指针移到至开始

	char *TotalSendPacket = new char[BUFF_SIZE];
	char szBuf[10240];
	char FileName[256] = { 0 };

	//从文件路径中提取文件名字
	int i = 0;
	char filename[256] = { 0 };
	const char *ptoslant = filepath;
	const char *ptmptoslant = ptoslant;
	while ((ptmptoslant = strstr(ptoslant, "\\")) != NULL)ptoslant = ptmptoslant + 1;
	while (filename[i++] = *ptoslant++);
	remove(filename);//为了测试方便,每次删除文件如果存在
	while (!input_file_stream.eof())
	{
		input_file_stream.read(szBuf, sizeof(char) * 10240);
		int realreadBytes = input_file_stream.gcount();//获取本次真正读到的字节
		if (input_file_stream.bad())
		{
			MessageBox("读取文件异常");
			break;
		}
		//将信息拷贝到总字符串处
		memcpy(TotalSendPacket, szBuf, 10240);
		memcpy(TotalSendPacket + 10240, filename, 256);
		memcpy(TotalSendPacket + 10240 + 256, &realreadBytes, sizeof(int));
		My_WriteFile(TotalSendPacket);
	}
	delete[] TotalSendPacket;
	MessageBox("Send Over!");
}
int CtestDlg::My_WriteFile(const char *sct)
{
	assert(sct != NULL);
	//分割字符串求得数据包内容,文件名字,以及读取到得实际字节数目。
	char szBuf[10240];//数据包值
	char FileName[256];//文件名字
	memcpy(szBuf, sct, 10240);

	memcpy(FileName, sct + 10240, 256);
	int realreadByes;
	memcpy(&realreadByes, sct + 10240 + 256, 4);
	


	ofstream out_file_stream;
	
	out_file_stream.open(FileName, std::ios::binary|std::ios::app);
	if (!out_file_stream)
	{
		MessageBox("写入文件打开失败");
		return -1;
	}
	out_file_stream.write(szBuf, realreadByes);
	out_file_stream.close();
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值