Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 测试istream对象_C++_Iostream_Istream - Fatal编程技术网

C++ 测试istream对象

C++ 测试istream对象,c++,iostream,istream,C++,Iostream,Istream,当我在测试中使用std::istream对象(在下面的cplusplus.com示例中,std::ifstream)时:“if(myistreamobject)”,堆栈中自动分配的对象永远不会为null,对吗?。。。在下面的示例中,我们使用相同的测试来检查是否从文件中读取了所有字节。。。这真是一个奇怪的代码,我在处理指针时通常使用这种风格 我想知道std::istream中使用哪种机制在测试中返回值,以及该值的真正含义。。。(最后一个操作的成功/失败???)是bool cast的重载(如MFC类

当我在测试中使用std::istream对象(在下面的cplusplus.com示例中,std::ifstream)时:“if(myistreamobject)”,堆栈中自动分配的对象永远不会为null,对吗?。。。在下面的示例中,我们使用相同的测试来检查是否从文件中读取了所有字节。。。这真是一个奇怪的代码,我在处理指针时通常使用这种风格

我想知道std::istream中使用哪种机制在测试中返回值,以及该值的真正含义。。。(最后一个操作的成功/失败???)是bool cast的重载(如MFC类CString中的const char*运算符cast)还是另一种技术

因为对象永远不会为null,所以将其放入测试将始终返回true

// 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) // <== this is really odd
      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;
}
//将文件读入内存
#include//std::cout
#include//std::ifstream
int main(){
std::ifstream是(“test.txt”,std::ifstream::binary);
如果(是){
//获取文件的长度:
is.seekg(0,is.end);
int length=is.tellg();
is.seekg(0,is.beg);
字符*缓冲区=新字符[长度];
标准::cout
测试它的
表达式
计算结果为
,这是一个布尔值。它适用于指针,因为
空ptr
/
/
0
计算结果为
,以及其他所有
。出于同样的原因,它适用于整数值

对于对象,它属于
操作符bool()
,请参阅

检查流是否没有错误

1) 如果fail()返回true,则返回null指针,否则返回非null指针。此指针可隐式转换为bool,并可在布尔上下文中使用

2) 如果流没有错误并且准备好进行I/O操作,则返回true。具体来说,返回!fail()

这个操作符使得可以使用返回流引用的流和函数作为循环条件,从而产生惯用的C++输入循环,如while(流>值){…},或(GETLIN(流,字符串)){…} ./P> 此类循环仅在输入操作成功时才执行循环体


std::istream
操作员已声明此权限:
显式运算符bool()const;

当你写作时
if(somestDistriamObject){…}
确实在调用
if(somestDistriamObject.operator bool())
没有检查非零

operator bool() 
如果流没有错误,则返回true,否则返回false

“无错误”概念与之前对流本身执行的操作有关

例如:在调用构造函数之后

std::ifstream is ("test.txt", std::ifstream::binary);
流对象中设置了一个内部状态标志。因此,当您调用运算符bool时,您将检查构造操作是否失败

此外,该方法

is.read(...)
还可以设置此内部状态标志,如中所示:

错误通过修改内部状态标志发出信号:eofbit、failbit、badbit

因此,在方法调用之后,如果流达到EOF(文件结束),则设置状态位,并且运算符bool将返回正值

这意味着在这种情况下,当您使用

if (is) { ... }
设置状态位后,将验证条件并执行if分支。

请阅读此处:
if (is) { ... }