什么时候`ifstream :: readsome`设置`eofbit`? [英] When does `ifstream::readsome` set `eofbit`?

查看:154
本文介绍了什么时候`ifstream :: readsome`设置`eofbit`?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码永远循环:

#include <iostream>
#include <fstream>
#include <sstream>

int main(int argc, char *argv[])
{
    std::ifstream f(argv[1]);
    std::ostringstream ostr;

    while(f && !f.eof())
    {
        char b[5000];
        std::size_t read = f.readsome(b, sizeof b);
        std::cerr << "Read: " << read << " bytes" << std::endl;
        ostr.write(b, read);
    }
}

这是因为 code>从不设置 eofbit

极其容易出错的cplusplus.com 说:


错误通过修改内部状态标志来指示:

Errors are signaled by modifying the internal state flags:

eofbit get指针位于流缓冲区内部当调用函数时,输入
数组,这意味着在内部缓冲区(可能或不是输入
序列的末尾)读取的位置不是
。当 rdbuf() - > in_avail()
第一个字符之前返回 -1

eofbit The get pointer is at the end of the stream buffer's internal input array when the function is called, meaning that there are no positions to be read in the internal buffer (which may or not be the end of the input sequence). This happens when rdbuf()->in_avail() would return -1 before the first character is extracted.

failbit 流位于
函数之前的字符源结尾

failbit The stream was at the end of the source of characters before the function was called.

badbit 发生上述以外的错误。

badbit An error other than the above happened.

几乎相同,标准说:


C ++ 11:27.7.2.3]: streamsize阅读器(char_type * s,streamsize n);

32。效果:作为未格式化的输入函数(如
27.7.2.3,第1段所述)。在构造sentry对象之后,如果!good()调用
setstate(failbit)异常和返回。否则提取
字符并将它们存储到数组的连续位置,其中第一个
元素由 s 指定。如果 rdbuf() - > in_avail()== -1 ,调用
setstate(eofbit) (可能会引发 ios_base :: failure (27.5.5.4)),并提取
无字符;

32. Effects: Behaves as an unformatted input function (as described in 27.7.2.3, paragraph 1). After constructing a sentry object, if !good() calls setstate(failbit) which may throw an exception, and return. Otherwise extracts characters and stores them into successive locations of an array whose first element is designated by s. If rdbuf()->in_avail() == -1, calls setstate(eofbit) (which may throw ios_base::failure (27.5.5.4)), and extracts no characters;


  • 如果 rdbuf() - > in_avail()== 0 ,不提取任何字符

  • 如果 rdbuf() - > in_avail()> 0 ,提取 min(rdbuf() - > in_avail(),n))

  • If rdbuf()->in_avail() == 0, extracts no characters
  • If rdbuf()->in_avail() > 0, extracts min(rdbuf()->in_avail(),n)).

33。

33. Returns: The number of characters extracted.

in_avail()== 0 条件是无操作意味着 ifstream :: readsome 本身是一个无操作,如果流缓冲区为空,但 in_avail()== -1 条件意味着设置 eofbit 已导致 in_avail()== -1

That the in_avail() == 0 condition is a no-op implies that ifstream::readsome itself is a no-op if the stream buffer is empty, but the in_avail() == -1 condition implies that it will set eofbit when some other operation has led to in_avail() == -1.

这似乎是不一致,甚至尽管 readsome的某些性质

This seems like an inconsistency, even despite the "some" nature of readsome.

那么的语义 readsome eof ?我正确地解释它们吗?

So what are the semantics of readsome and eof? Have I interpreted them correctly? Are they an example of poor design in the streams library?

(从[IMO]无效< a href =http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52169> libstdc ++错误52169 。)

推荐答案

我认为这是一个自定义点,默认的流实现并不真正使用。

I think this is a customization point, not really used by the default stream implementations.

in_avail()返回它在内部缓冲区中可以看到的字符数(如果有的话)。否则调用 showmanyc()尝试检测是否知道字符在其他地方可用,因此缓冲区填充请求将保证成功。

in_avail() returns the number of chars it can see in the internal buffer, if any. Otherwise it calls showmanyc() to try to detect if chars are known to be available elsewhere, so a buffer fill request is guaranteed to succeed.

反过来, showmanyc()将返回它知道的字符数,如果有的话,则返回-1,如果知道<

In turn, showmanyc() will return the number of chars it knows about, if any, or -1 if it knows that a read will fail, or 0 if it doesn't have a clue.

默认实现( basic_streambuf )总是返回0,所以这是你得到的,除非你有一个流与一些其他streambuf覆盖 showmanyc

The default implementation (basic_streambuf) always returns 0, so that is what you get unless you have a stream with some other streambuf overriding showmanyc.

你的循环本质上是多读的字符,当它是零(意思是不确定)时,它会被卡住。

Your loop is essentially read-as-many-chars-as-you-know-is-safe, and it gets stuck when that is zero (meaning "not sure").

这篇关于什么时候`ifstream :: readsome`设置`eofbit`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆