在 IOStreams 库中使用ignore(numeric_limits::max()) 是否可以处理任意大量流?

发布于 2024-11-04 14:11:42 字数 309 浏览 2 评论 0原文

在 C++ 标准(第 27.6.1.3\24 节)中,对于 IOStreams 库中的 istream ignore() 函数,这意味着如果您为 numeric_limits::max() 的“n”提供参数,它将继续忽略字符 永远直到找到分隔符,甚至远远超出实际 流大小的最大值(即“n”参数被解释为无限)。

对于 gcc 实现来说,这确实是这样的 ignore() 已实现,但我仍然不清楚 这是特定于实现的还是标准强制执行的。 熟悉这一点的人可以确认这是由一个保证的吗? 符合标准的 iostreams 库?

In the C++ standard (section 27.6.1.3\24), for the
istream ignore() function in the IOStreams library, it implies that if you supply an argument for 'n' of numeric_limits::max(), it will continue to ignore characters
forever up until the delimiter is found, even way beyond the actual
max value for streamsize (i.e. the 'n' argument is interpreted as infinite).

For the gcc implementation this does indeed appear to be how
ignore() is implemented, but I'm still unclear as to
whether this is implementation specific, or mandated by the standard.
Can someone who knows this well confirm that this is guaranteed by a
standard compliant iostreams library?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只有影子陪我不离不弃 2024-11-11 14:11:42

标准规定,numeric_limits::max() 是一个特殊值,不会影响跳过的字符数。

效果:表现为未格式化的输入函数(如 27.7.2.3 第 1 段中所述)。构造哨兵对象后,提取字符并丢弃它们。字符将被提取,直到发生以下任一情况:
-- 如果 n != numeric_limits::max() (18.3.2),则提取 n 个字符
-- 文件结尾发生在输入序列上(在这种情况下,函数调用 setstate(eofbit),这可能会抛出 ios_base::failure (27.5.5.4));
-- Traits::eq_int_type(traits::to_int_type(c), delim) 用于下一个可用输入字符 c(在这种情况下,将提取 c)。

The standard says that numeric_limits<streamsize>::max() is a special value that doesn't affect the number of characters skipped.

Effects: Behaves as an unformatted input function (as described in 27.7.2.3, paragraph 1). After constructing a sentry object, extracts characters and discards them. Characters are extracted until any of the following occurs:
-- if n != numeric_limits<streamsize>::max() (18.3.2), n characters are extracted
-- end-of-file occurs on the input sequence (in which case the function calls setstate(eofbit), which may throw ios_base::failure (27.5.5.4));
-- traits::eq_int_type(traits::to_int_type(c), delim) for the next available input character c (in which case c is extracted).

静赏你的温柔 2024-11-11 14:11:42

根据此处

istream&  istream::ignore ( streamsize n = 1, int delim = EOF );

提取和丢弃字符
从输入序列中提取字符并丢弃它们。

当 n 个字符被提取并丢弃时当找到字符 delim 时提取结束,以先到者为准。在后一种情况下,定界字符本身也会被提取。

在您的情况下,当达到 numeric_limits::max() 字符数时,就满足第一个条件。

[Per Bo]

但是,根据规范,仅当 n 等于 numeric_limits::max() 时才应用上述情况。

According to here:

istream&  istream::ignore ( streamsize n = 1, int delim = EOF );

Extract and discard characters
Extracts characters from the input sequence and discards them.

The extraction ends when n characters have been extracted and discarded or when the character delim is found, whichever comes first. In the latter case, the delim character itself is also extracted.

In your case, when numeric_limits::max() number of characters have been reached, the first condition is met.

[Per Bo]

However, according to spec, the above case is applied only when n is not equal to numeric_limits<streamsize>::max().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文