Does using ignore(numeric_limits<streamsize>::max()) in the IOStreams library handle arbitrarily massive streams?

和自甴很熟 提交于 2019-12-06 07:45:13

问题


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?


回答1:


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).




回答2:


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().



来源:https://stackoverflow.com/questions/5878052/does-using-ignorenumeric-limitsstreamsizemax-in-the-iostreams-library-ha

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!