Topic: streams
Author: "Yaron" <yhirsch@netvision.net.il>
Date: Thu, 18 Oct 2001 00:31:02 GMT Raw View
Hi,
Can anyone please explain to me the difference between the fail() bit and
the
bad() bit?
In addition, is there a way to force a stream to flush after taking in a
certain
amount of data?
Thanks
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: "Christian Brock" <brock@ivi.fhg.de>
Date: Thu, 18 Oct 2001 10:05:16 GMT Raw View
"Yaron" <yhirsch@netvision.net.il> schrieb im Newsbeitrag
news:9qifb9$92l$1@news.netvision.net.il...
> Hi,
> Can anyone please explain to me the difference between the fail() bit and
> the
> bad() bit?
The standart says in 27.4.2.1.3
badbit indicates a loss of integrity in an input or output sequence (such as
an irrecoverable read error from a file);
failbit indicates that an input operation failed to read the expected
characters, or that an output operation failed to generate the desired
characters.
e.g. basic_istream<charT,traits>& read(char_type* s, streamsize n); will set
failbit if eof is reached before less than n characters are extracted.
> In addition, is there a way to force a stream to flush after taking in a
> certain
> amount of data?
write your own stream buffer. Its not that hard and gives you control over
this kind of extra behavior.
Have fun, Chr.
--------------------------------------
Christian Brock <brock@ivi.fhg.de>
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: dietmar_kuehl@yahoo.com (Dietmar Kuehl)
Date: Thu, 18 Oct 2001 14:14:13 GMT Raw View
Hi,
"Yaron" <yhirsch@netvision.net.il> wrote:
> Can anyone please explain to me the difference between the fail() bit and
> the bad() bit?
'std::ios_base::failbit' is used for formatting problems, eg. a missing line
break when reading a line with 'getline()' or a non-digit received when
reading a number, while 'std::ios_base::badbit' is used for stream related
problems like failing to read from or write to a file. In general, you can
recover from errors causing 'failbit' to be set, eg. by ignoring the next
few characters or increasing a buffer, while you cannot recover from errors
causing 'badbit' eg. because it is [normally] something outside the control
of the program.
> In addition, is there a way to force a stream to flush after taking in a
> certain amount of data?
Yes, you can.
You also want to know how? The short answer here is: Use a filtering stream
buffer which fills a buffer of the desired size, then sents it to the
underlying stream buffer followed by a call to 'pubsync()'.
You can also set the formatting flag 'unitbuf' to cause the stream to be
flushed after each I/O operation. This means, of course, that the stream is
just flushed more frequently but not at specific amounts of data.
--
<mailto:dietmar_kuehl@yahoo.com> <http://www.dietmar-kuehl.de/>
Phaidros eaSE - Easy Software Engineering: <http://www.phaidros.com/>
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]