在cplusplus.com給出一個例子:不明白的IStream 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 :: basic_istream'轉換爲布爾值的語義的一部分。請參閱本頁底部的表格以瞭解詳細信息http://en.cppreference.com/w/cpp/io/basic_ios/operator_bool – StoryTeller