Delphi TFileStream.Seek,如何检查无效的查找偏移量
我正在 Delphi 2006 中使用 TFileStream。当我使用超出范围的偏移量调用 TFileStream.Seek 时,我得到不同的返回值。当我寻找到流开头下方的位置时,该函数返回 -1,如果我寻找超出流大小的位置,该函数将返回流中的位置(如果流那么大)。有没有办法检查流上的查找操作是否成功?当查找偏移量超出当前流大小的范围时,为什么 TFileStream.Seek 不会失败?
提前致谢。
I am working with TFileStream in Delphi 2006. When I invoke TFileStream.Seek with an offset that is out of bounds I am getting different return values. When I seek to a position below the beginning of the stream, the function returns -1 and if I seek to a beyond the stream size, the function returns what would have been the position in the stream if the stream was that large. Is there a way to check whether a seek operation on the stream was successful? Why does TFileStream.Seek not fail when the seek offsets are out of bounds of the current stream size?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,您可以超出文件大小进行查找 - 如果此处没有错误,则查找成功。除此之外,您还可以锁定文件区域(请参阅 LockFile< /a>) 超出文件大小 - 这也可以,并且某些 RDBMS 使用它来实现表/记录锁定。
同样来自 MSDN:
因此,通过设置超出文件大小的文件指针,您可以随后增加文件大小(例如通过 SetEndOfFile)。
Yes, you can seek beyond file size - where is no error here, the seek is successful. More than that, you can lock file region (see LockFile) beyond file size - that is also OK and is used by some RDBMS to implement table/record locking.
Also from MSDN:
So by setting a file pointer beyond the file size you can subsequently increase the file size (ex by the SetEndOfFile).
它调用一个 Windows 函数,并且您得到的结果来自该 Windows 函数。
如果 Seek 值有效,我倾向于检查您的代码。如果您需要大量执行此操作,那么可能会创建 TFileStream 的后代,例如 TRangeCheckingFileStream ,它在其查找方法中包含范围检查并返回您可以期望的值。
It calls a windows function and the result you get is from the windows function.
I would be inclined to check in your code if the Seek value is valid. If you need to do this alot then maybe create a descendant of TFileStream, something like TRangeCheckingFileStream which includes range checks in it's seek method and returns a value you can expect.