我无法解决这个问题。我不知道为什么我会收到此错误消息。
在 Project2.exe 中的 0x7642DEB5 (KernelBase.dll) 处引发异常:0xC0000005:访问冲突写入位置 0x00000000。
错误在ReadFile(file,lpBuffer, nNumberOfBytesToRead-1, NULL, NULL)
这是我的代码。我正在尝试访问 JPG 文件以读取其标题。
#include<Windows.h>
#include<iostream>
int main()
{
LPCTSTR path = "jpg.jpg";
DWORD nNumberOfBytesToRead;
HANDLE file = CreateFile(path, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (file == INVALID_HANDLE_VALUE)
{
std::cout << "The file couldnt be opened" << std::endl;
}
nNumberOfBytesToRead = GetFileSize(file, NULL);
BYTE *lpBuffer = new BYTE[nNumberOfBytesToRead-1] ;
if (ReadFile(file,lpBuffer, nNumberOfBytesToRead-1, NULL, NULL))
{
delete[] lpBuffer;
CloseHandle(file);
std::cout << "The file couldnt be read" << std::endl;
}
CloseHandle(file);
delete[] lpBuffer;
if (file != 0)
{
std::cout << "The file has been closed" << std::endl;
}
system("PAUSE");
return 0;
}
谢谢我已经解决了这个问题。我还有一个问题
lpBuffer = 0xcccccccc 读取字符串字符时出错。>
这是我的新代码。
#include<Windows.h>
#include<iostream>
int main()
{
LPCTSTR path = "jpg.jpg";
DWORD nNumberOfBytesToRead = 0;
DWORD nNumberOfBytesRead = 0;
HANDLE file = CreateFile(path, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (file == INVALID_HANDLE_VALUE)
{
std::cout << "The file couldnt be opened" << std::endl;
}
nNumberOfBytesToRead = GetFileSize(file, NULL);
BYTE *lpBuffer = new BYTE[nNumberOfBytesToRead];
if (ReadFile(file, lpBuffer, nNumberOfBytesToRead, &nNumberOfBytesRead, NULL))
{
std::cout << "The file couldnt be read" << std::endl;
}
CancelIo(file);
CloseHandle(file);
delete[] lpBuffer;
system("PAUSE");
return 0;
}