使用 ReadFile 时是否更新了重叠结构?
is the overlapped structure updated when using ReadFile?
我正在学习 win32 编程。
我在参考手册(此处:https://msdn.microsoft.com/en-us/library/windows/desktop/aa365467%28v=vs.85%29.aspx)上读到
If lpOverlapped is not NULL, the read operation starts at the offset that is specified in the OVERLAPPED structure and ReadFile does not return until the read operation is complete. The system updates the OVERLAPPED offset before ReadFile returns.
但是,如果我调用 ReadFile(hmyFile, &myrecord, sizeof(record_t), &n, &ov);
,我会看到值 ov.offset
保持不变。为何如此?我哪里误解了参考手册中的内容?
更多详情:
文件处理程序打开为 hmyFile = CreateFile(argv[1], GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
我没有使用 FILE_FLAG_OVERLAPPED
如评论所示,这是文档中的一个错误。实际行为是更新文件指针,就像 lpOverlapped 为 NULL 且句柄同步时一样。旧版本的文档是正确的。以下摘自 2000 年 7 月版的 Platform SDK 文档:
The ReadFile function reads data from a file, starting at the position
indicated by the file pointer. After the read operation has been completed,
the file pointer is adjusted by the number of bytes actually read, unless the
file handle is created with the overlapped attribute. [...]
If hFile is not opened with FILE_FLAG_OVERLAPPED and lpOverlapped is not NULL,
the read operation starts at the offset specified in the OVERLAPPED structure.
ReadFile does not return until the read operation has been completed.
我正在学习 win32 编程。 我在参考手册(此处:https://msdn.microsoft.com/en-us/library/windows/desktop/aa365467%28v=vs.85%29.aspx)上读到
If lpOverlapped is not NULL, the read operation starts at the offset that is specified in the OVERLAPPED structure and ReadFile does not return until the read operation is complete. The system updates the OVERLAPPED offset before ReadFile returns.
但是,如果我调用 ReadFile(hmyFile, &myrecord, sizeof(record_t), &n, &ov);
,我会看到值 ov.offset
保持不变。为何如此?我哪里误解了参考手册中的内容?
更多详情:
文件处理程序打开为 hmyFile = CreateFile(argv[1], GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
我没有使用 FILE_FLAG_OVERLAPPED
如评论所示,这是文档中的一个错误。实际行为是更新文件指针,就像 lpOverlapped 为 NULL 且句柄同步时一样。旧版本的文档是正确的。以下摘自 2000 年 7 月版的 Platform SDK 文档:
The ReadFile function reads data from a file, starting at the position indicated by the file pointer. After the read operation has been completed, the file pointer is adjusted by the number of bytes actually read, unless the file handle is created with the overlapped attribute. [...]
If hFile is not opened with FILE_FLAG_OVERLAPPED and lpOverlapped is not NULL, the read operation starts at the offset specified in the OVERLAPPED structure. ReadFile does not return until the read operation has been completed.