从文件读取问题导致无限循环
好吧,我正在开发的这个程序似乎一切正常,除了有一个问题。这是代码
#include <iostream>
#include <fstream>
using namespace std;
/*
Function Name: CalculateBinary
CalculateBinary takes a number from the main function and finds its binary form.
*/
void CalculateBinary( long InputNum)
{
//Takes InputNum and divides it down to "1" or "0" so that it can be put in binary form.
if ( InputNum != 1 && InputNum != 0)
CalculateBinary(InputNum/2);
// If the number has no remainder it outputs a "0". Otherwise it outputs a "1".
if (InputNum % 2 == 0)
cout << "0";
else
cout << "1";
}
void main()
{
// Where the current number will be stored
long InputNum;
//Opens the text file and inputs first number into InputNum.
ifstream fin("binin.txt");
fin >> InputNum;
// While Input number is not 0 the loop will continue to evaluate, getting a new number each time.
while (InputNum >= 0)
{
if(InputNum > 1000000000)
cout << "Number too large for this program ....";
else
CalculateBinary(InputNum);
cout << endl;
fin >> InputNum;
}
}
这是我正在读取的文本文件
12
8764
2147483648
2
-1
当我到达 8764 时,它只是一遍又一遍地读取这个数字。它忽略 2147483648。我知道我可以通过将 InputNum 声明为 long long 来解决这个问题。但我想知道它为什么这样做?
Ok this program I am working on seems to be all ok except there is a problem. Here is the code
#include <iostream>
#include <fstream>
using namespace std;
/*
Function Name: CalculateBinary
CalculateBinary takes a number from the main function and finds its binary form.
*/
void CalculateBinary( long InputNum)
{
//Takes InputNum and divides it down to "1" or "0" so that it can be put in binary form.
if ( InputNum != 1 && InputNum != 0)
CalculateBinary(InputNum/2);
// If the number has no remainder it outputs a "0". Otherwise it outputs a "1".
if (InputNum % 2 == 0)
cout << "0";
else
cout << "1";
}
void main()
{
// Where the current number will be stored
long InputNum;
//Opens the text file and inputs first number into InputNum.
ifstream fin("binin.txt");
fin >> InputNum;
// While Input number is not 0 the loop will continue to evaluate, getting a new number each time.
while (InputNum >= 0)
{
if(InputNum > 1000000000)
cout << "Number too large for this program ....";
else
CalculateBinary(InputNum);
cout << endl;
fin >> InputNum;
}
}
Here is the text file I am reading in
12
8764
2147483648
2
-1
When I get to 8764, it just keeps reading in this number over and over again. It ignores the 2147483648. I know I can solve this by declaring InputNum as a long long. But I want to know why is it doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是您编写的此类循环的常见问题。
正确且惯用的循环是这样的:
That is the usual problem with such loops which you've written.
The correct and the idiomatic loop is this:
这个数字对于
long
来说太大了,无法存储,所以fin >>> InputNum;
什么也不做。您应该始终读作while(fin >> InputNum) { ... }
,因为这将在失败时立即终止循环,或者至少检查流状态。That number is too large for a
long
to store, sofin >> InputNum;
does nothing. You should always read aswhile(fin >> InputNum) { ... }
, as that will terminate the loop immediately on failure, or at least check the stream state.您的平台上的
long
类型似乎是 32 位宽。数字 2147483648 (0x80000000) 太大,无法表示为有符号 32 位整数。您要么需要无符号类型(显然不适用于负数),要么需要 64 位整数。另外,您应该检查读取是否成功:
It would appear that the
long
type on your platform is 32 bits wide. The number 2147483648 (0x80000000) is simply too large to be represented as a signed 32-bit integer. You either need an unsigned type (which obviously won't work with negative numbers) or a 64-bit integer.Also, you should check whether the read is successful:
您不检查 EOF,因此永远陷入循环中。
鳍>>如果成功,InputNum
表达式返回true
,否则返回false
,因此将代码更改为类似以下内容将解决问题:You don't check for EOF, thus being trapped in a loop forever.
fin >> InputNum
expression returnstrue
if succeeded,false
otherwise, so changing you code to something like this will solve the problem: