当>>运算符试图输入一个大于变量可以包含的值? [英] What happens when >> operator is trying to input a value bigger than a variable can contain?

查看:54
本文介绍了当>>运算符试图输入一个大于变量可以包含的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要从文本文件中提取数字,并用它们填充int类型的数组.

I'm pulling numbers from a text file and filling an array of type int with them.

我将这些值插入数组,同时使用这些代码行(其中k是.txt文件中的数字量)循环遍历.txt文件:

I'm inserting the values into the array while looping through the .txt file with those lines of code (where k is the amount of numbers in the .txt file):

for (int j = 0; j < k; j++)
  inputFile >> tab[j];

当文本文件中的数字小于2,147,483,647(这是整数类型的最大大小)时,一切都会顺利进行.

When the numbers in the text file are less than 2,147,483,647 which is the maximum size of an integer type everything goes smooth.

当数字大于该数字时,我认为程序溢出并且无法插入,但是在此之后也无法插入任何数字.

When the number is bigger than this the program as i assume overflows and fails to insert it but it also fails to insert any number after that.

是什么导致它在发生溢出后不再插入任何数字?

What is causing it to not insert any more numbers after the overflow happens?

推荐答案

std::istream& std::istream::operator>>(std::istream&, int&) ,cppreference说:

On std::istream& std::istream::operator>>(std::istream&, int&), cppreference says:

表现为FormattedInputFunction.在构造并检查了可能跳过前导空格的哨兵对象之后,通过调用std::num_get::get()

...

如果提取失败,则将零写入值并设置故障位.如果提取导致该值太大或太小而无法容纳该值,则写入std::numeric_limits<T>::max()std::numeric_limits<T>::min()并设置故障位标志. (自c ++ 11起)

If extraction fails, zero is written to value and failbit is set. If extraction results in the value too large or too small to fit in value, std::numeric_limits<T>::max() or std::numeric_limits<T>::min() is written and failbit flag is set. (since c++11)

这不是规范性的,但确实提供了有关此处发生情况的不错的总结.

It's not normative, but it does provide a decent summary of what is happening here.

FormattedInputFunctions将根据流构造一个哨兵并检查其值.如果哨兵对象的评估结果为false,则不执行输入.

FormattedInputFunctions will construct a sentry from the stream and check the value. If the sentry object evaluates as false, then no input is performed.

如果在其他情况下,正在操作的流已设置了故障位.

The sentry object will evaluate as false if, among other situations, the stream being operated on has the failbit set.

所以,这是怎么回事:

  1. 流尝试读取太大而无法容纳int数据类型的整数.
  2. 传递给函数的int设置为int可以存储的最大可能值.
  3. 传递给函数的流已设置其故障位.
  4. 进一步读取流失败并且什么也不做,因为设置了流的故障位.
  1. The stream tries to read in an integer too large to hold in the int data type.
  2. The int passed into its function is set to the maximum possible value an int can store.
  3. The stream passed into the function has its failbit set.
  4. Further reads to the stream fail and do nothing, because the stream's failbit is set.

如接受的问题

You can detect these overflow errors by checking the failbit and value of the integer after the read operation you are performing, as mentioned in the accepted answer to the question Read int from istream, detect overflow.

您可以通过取消使用

You can recover from these errors by unsetting the failbit with std::basic_ios::clear. Following a call to clear which unsets the failbit, further reads will behave as you expect.

这篇关于当&gt;&gt;运算符试图输入一个大于变量可以包含的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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