.h中
class CDlgDlg : public CDialog中定义了4个变量: BYTE *pBufIn;
BYTE *pBufOut;
CMemFile *pFileIn;
CMemFile *pFileOut;.cpp中
BOOL CDlgDlg::OnInitDialog()加入
pBufIn=new BYTE[1024];
pBufOut=new BYTE[1024];
pFileIn=new CMemFile(pBufIn,1024,256);
pFileOut=new CMemFile(pBufIn,1024,256);
按钮1代码:
void CDlgDlg::OnButton1()
{
// TODO: Add your control notification handler code here
CArchive arIn(pFileIn,CArchive::store);
CString str="hello";
arIn<<str;
arIn.Close();
int i=pFileIn->GetLength();
memcpy(pBufOut,pBufIn,1024);
}
按钮2代码:
void CDlgDlg::OnButton2()
{
// TODO: Add your control notification handler code here
CArchive arOut(pFileOut,CArchive::load);
int i=pFileOut->GetLength();
CString str;
arOut>>str;
arOut.Close();
}
我先按下按钮1,再按下按钮2时,会造成一个错误:
vc6会自动弹出一个错误对话框,而且还会显示中文:试图越过其尾端对一 未命名文件 进行读写因为我这种代码要用在网络上传输,一定要定义2个buf(服务器一个,客户端一个),请问我该如何改正?我为什么要这么改?
class CDlgDlg : public CDialog中定义了4个变量: BYTE *pBufIn;
BYTE *pBufOut;
CMemFile *pFileIn;
CMemFile *pFileOut;.cpp中
BOOL CDlgDlg::OnInitDialog()加入
pBufIn=new BYTE[1024];
pBufOut=new BYTE[1024];
pFileIn=new CMemFile(pBufIn,1024,256);
pFileOut=new CMemFile(pBufIn,1024,256);
按钮1代码:
void CDlgDlg::OnButton1()
{
// TODO: Add your control notification handler code here
CArchive arIn(pFileIn,CArchive::store);
CString str="hello";
arIn<<str;
arIn.Close();
int i=pFileIn->GetLength();
memcpy(pBufOut,pBufIn,1024);
}
按钮2代码:
void CDlgDlg::OnButton2()
{
// TODO: Add your control notification handler code here
CArchive arOut(pFileOut,CArchive::load);
int i=pFileOut->GetLength();
CString str;
arOut>>str;
arOut.Close();
}
我先按下按钮1,再按下按钮2时,会造成一个错误:
vc6会自动弹出一个错误对话框,而且还会显示中文:试图越过其尾端对一 未命名文件 进行读写因为我这种代码要用在网络上传输,一定要定义2个buf(服务器一个,客户端一个),请问我该如何改正?我为什么要这么改?
pFileOut=new CMemFile(pBufIn,1024,256);
你这两个MemFile需要指向同一个空间么?还是写错了?
1 只有先写文件才能读,你的第一步操作是复制文件attch的内存,并没有对文件操作,所以pFileOut的文件大小为0,当然就不能读取
2 读文件要先把文件指针移动到头部,或者你需要的地方
void CDlgDlg::OnButton2()
{
// TODO: Add your control notification handler code here
CArchive arOut(pFileOut,CArchive::load);
int i=pFileOut->GetLength();
CString str;
arOut>>str;
arOut.Close();
}改为void CDlgDlg::OnButton2()
{
// TODO: Add your control notification handler code here //恢复文件指针,从头开始读
//
pFileIn->SeekToBegin(); CArchive arOut(pFileIn,CArchive::load);
int i=pFileOut->GetLength();
CString str;
arOut>>str;
arOut.Close();
}
处理2(中间结果文件1,中间结果文件2)
处理3(中间结果文件2,中间结果文件3)
处理4(中间结果文件3,最终结果文件)
你给我改的这些代码,都是在操作相同的buf。我现在这个功能主要用在通讯程序上,服务器和客户端的buf肯定是不同的,CMemFile建立的对象也不会相同,这时候该如何反序列化?
你给我改的这些代码,都是在操作相同的buf。我现在这个功能主要用在通讯程序上,服务器和客户端的buf肯定是不同的,CMemFile建立的对象也不会相同,这时候该如何反序列化?
莫有问题,楼主看仔细了,两个buf虽然名字一样,但是都是本地变量,换个buf变量名不影响
CSocketFile呢,而你这样处理只能用来MFC之间通讯,对于非MFC程序就不行了void CDlgDlg::OnButton1()
{
//主要的改的是CArchive 的定义,要把相应的buffer传进去,否则它会自己申请内存,用的不是你file关联的内存
CArchive arIn(pFileIn,CArchive::store,1024,pBufIn);
CString str="hello";
arIn<<str;
arIn.Close();
//这个长度需要保存下来,在下面用
int i=pFileIn->GetLength();
memcpy(pBufOut,pBufIn,1024);
}void CDlgDlg::OnButton2()
{
//这句一定要加上,就是上面那个文件大小,这里直接写6了
pFileOut->SetLength(6);
CArchive arOut(pFileOut,CArchive::load,1024,pBufOut);
int i=pFileOut->GetLength();
CString str;
arOut>>str;
arOut.Close(); AfxMessageBox(str);
}