不明白 istream::read cplusplus.com 示例
Don't understand cplusplus.com example for istream::read
在 cplusplus.com 给出了一个例子:
// read a file into memory
#include <iostream> // std::cout
#include <fstream> // std::ifstream
int main () {
std::ifstream is ("test.txt", std::ifstream::binary);
if (is) {
// get length of file:
is.seekg (0, is.end);
int length = is.tellg();
is.seekg (0, is.beg);
char * buffer = new char [length];
std::cout << "Reading " << length << " characters... ";
// read data as a block:
is.read (buffer,length);
if (is)
std::cout << "all characters read successfully.";
else
std::cout << "error: only " << is.gcount() << " could be read";
is.close();
// ...buffer contains the entire file...
delete[] buffer;
}
return 0;
}
有人可以解释为什么最后一个if (is)
可以确定是否读取了所有字符吗?如果我们已经进入的陈述以及我解释它的方式(可能过于简单和错误(我们只检查是否存在,这是一样的,但这不是已经建立了吗?
std::ifstream
有一个转换运算符来bool
,它返回是否在流上设置了badbit
或failbit
。
它本质上是if (!is.fail()) {/*...*/}
的简写。
std::ifstream
定义了operator bool() const
,它隐式地将流转换为布尔值。
从运算符 bool(( 的 cplusplus.com
:返回是否设置了错误标志(故障位或坏位(。
请注意,此函数不返回与成员 good 相同的内容,但 成员失败的反义词。
http://www.cplusplus.com/reference/ios/ios/operator_bool/
相关文章:
- 激励'inline'说明符的真实世界示例?
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 为什么示例代码访问IUnknown中已删除的内存
- 以下示例中如何避免代码复制?C++/库达
- "类模板示例<int>;"语句对 C++11 是什么意思?
- 示例C++项目编译中的警告
- 示例外壳应用程序显示的 V8 "segmentation fault (core dumped)"错误
- 当Microsoft文档仅包含 C# 示例时,如何查找 C++ 包含文件名
- 两个 COM 组件中 ENUM 的重复条目
- COM 接口 c# 封送数组数组
- 在简单示例中,Python3 + ctypes 回调会导致内存泄漏
- 为什么理解这个递归示例如此难以转化为直觉?
- COM :是否可以查看是否存在对我的某个 COM 对象的进程外引用?我可以释放它吗?
- Eventloop 卡住:NAO C++ SDK OnFaceDetection 示例
- chaiscript.com 的基本示例在运行时引发异常
- 不明白 istream::read cplusplus.com 示例
- 在C++Builder中有COM事件处理的工作示例吗
- 无法编译Microsoft COM/ActiveX示例
- ActiveQt Com应用程序示例- Com服务器未在Windows注册表中注册(Qt4.7.4)
- printf from Nucleo to Windows-7 virtual COM 在 X-NUCLEO-IKS01A1 示例中不起作用