gpt4 book ai didi

c++ - 异步读取文件 - iso 文件 C++

转载 作者:行者123 更新时间:2023-11-30 03:03:25 26 4
gpt4 key购买 nike

在我的 C++ 应用程序中,我尝试通过 createfile 异步读取 iso 文件 - 使用重叠标志,然后是 readfile。然而,当我在一个简单的文件(例如 txt 文件)上尝试这段代码时,它起作用了。但是当我在 iso 文件上运行这段代码时 - 它失败了。我在 MSDN 中看到压缩文件只能通过 readfile 同步调用读取。 iso文件属于这一类吗?如果是 - 你有其他建议如何异步读取 iso 文件吗?

这是我的代码:

int _tmain(int argc, _TCHAR* argv[])
{


HANDLE hFile;
DWORD NumberOfBytesRead = 0, dw;
BYTE *buf = (BYTE*)malloc(BUF_SIZE*sizeof(BYTE));
OVERLAPPED overlapped;
overlapped.Offset = overlapped.OffsetHigh = 0;
memset(buf, 0, 1024);

overlapped.hEvent = CreateEvent(NULL, true, false, NULL);
if(NULL == overlapped.hEvent)
printf("error");

hFile = CreateFile("xxx.iso",
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED | FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING ,
NULL);



if (hFile == INVALID_HANDLE_VALUE)
printf("invalid hfile\n");

int i;
i= ReadFile(hFile,
buf,
BUF_SIZE,
&NumberOfBytesRead,
&overlapped);
if( GetLastError() == ERROR_IO_PENDING)
{


dw = WaitForSingleObject(overlapped.hEvent, INFINITE);
if(dw == WAIT_OBJECT_0)
if (GetOverlappedResult(hFile,&overlapped,&NumberOfBytesRead, TRUE) != 0)
{
if (NumberOfBytesRead != 0)
{
printf("!!!\n");
}

}

}

谢谢

最佳答案

您尚未发布您为 BUF_SIZE 常量使用的值,但请确保它是卷扇区大小的整数倍。这是使用无缓冲文件流时的常见陷阱。 CreateFile() documentationFILE_FLAG_NO_BUFFERING 的文档说:

There are strict requirements for successfully working with files opened with CreateFile using the FILE_FLAG_NO_BUFFERING flag, for details see File Buffering.

文件缓冲注意事项页面:

As previously discussed, an application must meet certain requirements when working with files opened with FILE_FLAG_NO_BUFFERING. The following specifics apply:

  • File access sizes, including the optional file offset in the OVERLAPPED structure, if specified, must be for a number of bytes that is an integer multiple of the volume sector size. For example, if the sector size is 512 bytes, an application can request reads and writes of 512, 1,024, 1,536, or 2,048 bytes, but not of 335, 981, or 7,171 bytes.

  • File access buffer addresses for read and write operations should be physical sector aligned, which means aligned on addresses in memory that are integer multiples of the volume's physical sector size. Depending on the disk, this requirement may not be enforced.

Application developers should take note of new types of storage devices being introduced into the market with a physical media sector size of 4,096 bytes.

在我的系统上,此值为 4K,一次读取任何小于 4K 的内容都会产生错误。在 Microsoft 的许多代码示例中,1K 是默认缓冲区大小,因此适配示例通常会导致无缓冲 I/O 错误。

编辑:还要确保将OVERLAPPED 结构的所有成员清零。您没有将 InternalInternalHigh 成员设置为 0。始终按以下方式清除 OVERLAPPED 结构:

OVERLAPPED overlapped;
ZeroMemory(&overlapped, sizeof(OVERLAPPED));

然后,您可以设置文件偏移量和事件句柄。

编辑:还要考虑以下关于 ReadFile()lpNumberOfBytesRead 参数的注释:

Use NULL for this parameter if this is an asynchronous operation to avoid potentially erroneous results. [...] For more information, see the Remarks section.

关于c++ - 异步读取文件 - iso 文件 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9373632/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com