把文件中的数据读到内存中,失败.

来源:百度知道 编辑:UC知道 时间:2024/06/21 10:09:25
void CcombinationDlg::readFile()
{
DWORD dwByteRead = 0;
DWORD dwByteWritten = 0;

HANDLE hFile = CreateFile(_T("D:\\test\\a.JPG"),GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
if ( hFile == INVALID_HANDLE_VALUE )
{
MessageBox(_T("Could not read file"), _T("Error"),MB_OK);
return;
}
filesizeOne = GetFileSize(hFile,NULL);
int error = GetLastError();
byteOne = new byte[filesizeOne]; // allocate space to store the file's data

if ( FALSE == ReadFile(hFile,byteOne,filesizeOne,&dwByteWritten,NULL) ) //filesizeOne 为2,877,739.读到byteOne中的内容只是几个字节的数据.
{
MessageBox(_T("Could not write file!"),_T("error"),MB_OK);
return;
}

//-----------------------------------------------------------------------------------------------------------------------

hFile = CreateFil

ReadFile(hFile,byteOne,filesizeOne,&dwByteWritten,NULL) 之后dwByteWritten值为多少?

建议你这样:
BOOL bResult=ReadFile(hFile,byteOne,filesizeOne,&dwByteWritten,NULL);
if(!bResult || dwByteWritten!=filesizeOne )
{
cout<< GetLastError();//查一下这个错误码(VC:工具-》错误查找)

}

//顺便说下 GetLastError();返回的是DWORD不是INT

=======================================================
你调用add时,自己传了错误的长度过去,当然不会写入任何数据。你调试一下WriteFile时你传的第三个参数是多少?连负数都有。。
另外:你的程序有严重的内存泄漏:
文件句柄没有closehandle;
点击几次read按钮,就会申请几次内存,而上一次申请的就蒸发了。