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;
}