本文整理汇总了C++中Istream::bad方法的典型用法代码示例。如果您正苦于以下问题:C++ Istream::bad方法的具体用法?C++ Istream::bad怎么用?C++ Istream::bad使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Istream
的用法示例。
在下文中一共展示了Istream::bad方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: test_readsome
void test_readsome (const charT *cbuf, const Traits*,
unsigned cbuf_size,
unsigned i, // index into states
unsigned j, // number of chars to read
unsigned k, // when underflow() will fail
int l, // value returned from showmanyc()
unsigned m) // how underflow should fail()
{
typedef std::basic_istream<charT, Traits> Istream;
static unsigned iter = 0; // iteration counter
// construct a stream buffer object and initialize its read
// sequence with the character buffer
Streambuf<charT, Traits> sb (cbuf, cbuf + cbuf_size - 1);
sb.showmanyc_ = l;
const char* err_type = 0;
if (m < 1) {
// have the stream buffer object's underflow() fail (by
// throwing an exception if possible) after k characters
// have been extracted (this object calls underflow() for
// every char)
sb.throw_after_ = k;
err_type = "threw";
}
else {
// have the stream buffer object's underflow() fail by
// returning eof after k characters have been extracted
// (this object calls underflow() for every char)
sb.fail_after_ = k;
err_type = "returned EOF";
}
// construct an istream object and initialize it with the user
// defined streambuf object
Istream is (&sb);
// set the stream object's initial state
is.setstate (states [i]);
// the number of extracted whitespace characters expected to
// be reported by gcount() must equal the number of successful
// extractions computed by the the stream buffer overridden
// underflow member function
const int extract =
is.good () ? j < k ? int (j) < l ? j
: l < 0 ? 0 : l
: int (k) < l ? k
: l < 0 ? 0 : l
: 0;
charT buf [256] = { 0 };
std::streamsize nread = -1;
// format the FCALL environment variable so that it can be
// conveniently used in diagnostics below
rw_fprintf (0, "%u. %{$FCALL!:@}",
iter,
"%{$ISTREAM}(%{*Ac}).readsome(%p, %d)",
int (sizeof *cbuf), cbuf, buf, j);
#ifndef _RWSTD_NO_EXCEPTIONS
const char *caught = 0;
// on every other iteration set badbit in exceptions to check
// that the thrown object is propagated by the function
if (k % 2 && !is.bad ())
is.exceptions (std::ios_base::badbit);
try {
nread = is.readsome (buf, j);
}
catch (const char *s) {
caught = s;
}
catch (...) {
caught = "...";
rw_assert (0, 0, __LINE__,
"%{$FCALL} threw an exception of unexpected type");
}
//////////////////////////////////////////////////////////////////
// verify that the function propagates exceptions thrown from the
// streambuf object only when badbit is set in the stream object's
// exceptions()
rw_assert (!caught || (k % 2), 0, __LINE__,
"%{$FCALL} unexpectedly propagated an exception");
#else // if defined (_RWSTD_NO_EXCEPTIONS)
nread = is.readsome (buf, j);
//.........这里部分代码省略.........