[英]To check SetFilePointerEx, ReadFile or WriteFile for success do I need to check lpNewFilePointer, lpNumberOfBytesRead and lpNumberOfBytesWritten?
I'm somewhat puzzled by SetFilePointerEx, ReadFile and WriteFile APIs. 我对SetFilePointerEx,ReadFile和WriteFile API感到有些困惑。 Say, if I want to move file pointer to a new position, is it sufficient to do:
说,如果我想将文件指针移动到新位置,是否足够:
if(SetFilePointerEx(hFile, liPtr, NULL, FILE_BEGIN))
{
//Success, moved file pointer to liPtr position
}
Or, do I need to check the value returned in lpNewFilePointer
too, as such? 或者,我是否也需要检查
lpNewFilePointer
返回的值?
LARGE_INTEGER liSetTo = {0};
if(SetFilePointerEx(hFile, liPtr, &liSetTo, FILE_BEGIN) &&
liPtr.QuadPart == liSetTo.QuadPart)
{
//Success
}
The same applies to ReadFile and WriteFile. 同样适用于ReadFile和WriteFile。 For instance:
例如:
if(WriteFile(hFile, buffer, numberBytesToWrite, NULL, NULL))
{
//Success writing numberBytesToWrite into file
}
Or do I need to do this to make sure that all my data was written successfully: 还是我需要这样做以确保所有数据都已成功写入:
DWORD numberBytesWritten = 0;
if(WriteFile(hFile, buffer, numberBytesToWrite, &numberBytesWritten, NULL) &&
numberBytesWritten == numberBytesToWrite)
{
//Success writing numberBytesToWrite into file
}
In other words, what's the point to have those return sizes and offsets? 换句话说,具有这些返回值大小和偏移量的意义何在? I mean, if I want it to write 1024 bytes into a file, can it just write 1000 instead.
我的意思是,如果我想让它向文件中写入1024个字节,可以改为写入1000个字节。 :) If it doesn't write all the data that I requested it to, wouldn't it constitute an error, or
FALSE
to be returned from the API? :)如果它没有写入我请求的所有数据,那么它不会构成错误,还是从API返回
FALSE
?
Anyway, I'd appreciate if someone could clarify. 无论如何,如果有人可以澄清,我将不胜感激。
For SetFilePointerEx
, you should only check it's return value. 对于
SetFilePointerEx
,您应该只检查它的返回值。 It contains whether the operation was a SUCCESS. 它包含操作是否为成功。
For a synchronous ReadFile
/ WriteFile
, if it returns false, then the operation has completely failed (no bytes read/written at all). 对于同步的
ReadFile
/ WriteFile
,如果返回false,则操作完全失败(根本没有读取/写入字节)。 If it returns true, then you should check lpNumberOfBytesRead
/ lpNumberOfBytesWritten
, as it can be lower then the number you specified. 如果返回true,则应检查
lpNumberOfBytesRead
/ lpNumberOfBytesWritten
,因为它可以低于您指定的数字。 For example, for read, if EOF reached, then you'll get a short read. 例如,对于阅读,如果达到EOF,您将获得简短阅读。 For write, if disk becomes full during write, you may get a short write.
对于写操作,如果在写操作期间磁盘已满,则可能会进行短暂的写操作。
There could be other various reasons for short read/write, so your code should handle these cases. 短读/写可能还有其他各种原因,因此您的代码应处理这些情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.