有没有办法验证并探测 read() 方法是否对文件进行了完美读取?
我想验证读取是否对文件进行了完美读取,有什么办法吗?
im looking to verify that read made a perfect read on a file, is there a way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
检查状态标志:
或者检查是否抛出 ios_base::failure 异常
Check for the state flags:
Or check if it throws
ios_base::failure
exception不确定“完美读取”是什么意思,但是如果满足以下条件,
failbit
将被设置流无法读取请求的字符数;在
换句话说,如果您请求 20 个字节,但只有 19 个字节可用,则
则视为读取失败。
测试失败的常用方法是将流视为
布尔值,例如:
or
(这也考虑了
badbit
,如果存在则将设置该值是读取时实际的 I/O 问题。)
对于 istream::read 的情况,您可能还需要检查
istream::gcount
以防失败;istream::gcount
返回最后一次未格式化读取所读取的字符数,即使在以下情况下
失败。因此,在读取原始数据时,通常会使用
类似于:
这与通常的习语略有不同,通常的习语一旦
输入失败。
Not sure what you mean by "perfect read", but
failbit
will be set ifthe stream was unable to read the requested number of characters; in
other words, if you ask for 20 bytes, and only 19 are available, the
read is deemed to have failed.
The usual way of testing for failure is simply by treating the stream as
a boolean, e.g.:
or
(This also takes
badbit
into consideration, which will be set if thereis an actual I/O problem when reading.)
In the case of
istream::read
, you may also want to checkistream::gcount
in case of failure;istream::gcount
returns thenumber of characters read by the last unformatted read, even in case of
failure. Thus, when reading raw data, it's not unusually to use
something like:
This is slightly different from the usual idiom, which stops as soon as
the input fails.