程序是在本地拷贝文件。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!");}

解决方案 »

  1.   

    如下修改:
    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;
                      buf = new char[size]; for (; total != length; )
    {
    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!");}因为VC在实现方面和标准C++有区别。
      

  2.   

    file1.Write(buf, uBytesRead);这句话也可能错误呢
      

  3.   

    #include "io.h"FILE *hSFile, hDFile;
    hSFile = fopen("123.Text", "rt");
    if (hSFile == NULL)
    return;
    hDFile = fopen("12354.Txt", "w+t");
    if (hDFile == NULL)
    {
    fclose(hSFile);
    return;
    }
    LONG nLength = _filelength(_fileno(hSFile));byte *buf = new byte[nLength];
    if (buf == NULL)
    {
    fclose(hSFile);
    fclose(hDFile);
    }fread(buf, 1, nLength, hSFile);
    fclose(hSFile);
    fwrite(buf, 1, nLength, hDFile);
    fclose(hDFile);
      

  4.   

    少了一句LONG nLength = _filelength(_fileno(hSFile));byte *buf = new byte[nLength];
    if (buf == NULL)
    {
    fclose(hSFile);
    fclose(hDFile);
    }fread(buf, 1, nLength, hSFile);
    fclose(hSFile);
    fwrite(buf, 1, nLength, hDFile);
    fclose(hDFile);
    //////
    delete []buf;