这个程序有内存错误,怎么修改?
程序是在本地拷贝文件。
void CFileDlg::OnButton1()
{
// TODO: Add your control notification handler code here
const int size = 512;
char * buf;
CString s;
UpdateData();
CFile file,file1;
file1.Open((LPCTSTR) m_edit2, CFile::modeCreate | CFile::modeWrite);
if (file.Open((LPCTSTR) m_edit1, CFile::modeRead))
{
int length = file.GetLength();
int uBytesRead = 0;
int total = 0;
for (; total != length; )
{
buf = new char[size];
uBytesRead = file.Read(buf,size);
total += uBytesRead;
buf[uBytesRead] = '\0';
file1.Write(buf, uBytesRead);
delete[] buf;// 这里有错
}
file1.Close();
file.Close();
}
else AfxMessageBox("No file found!");
}