从文件读取问题导致无限循环

发布于 2024-12-04 04:13:27 字数 1416 浏览 0 评论 0原文

好吧,我正在开发的这个程序似乎一切正常,除了有一个问题。这是代码

#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 技术交流群。

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

发布评论

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

评论(4

葬シ愛 2024-12-11 04:13:27

这是您编写的此类循环的常见问题。

正确且惯用的循环是这样的:

ifstream fin("binin.txt");
long InputNum;
while (fin >> InputNum && InputNum >= 0)
{
   //now construct the logic accordingly!
    if(InputNum > 1000000000)
         cout << "Number too large for this program ....";
    else
         CalculateBinary(InputNum);
    cout << endl;
}

That is the usual problem with such loops which you've written.

The correct and the idiomatic loop is this:

ifstream fin("binin.txt");
long InputNum;
while (fin >> InputNum && InputNum >= 0)
{
   //now construct the logic accordingly!
    if(InputNum > 1000000000)
         cout << "Number too large for this program ....";
    else
         CalculateBinary(InputNum);
    cout << endl;
}
花海 2024-12-11 04:13:27

这个数字对于 long 来说太大了,无法存储,所以 fin >>> InputNum; 什么也不做。您应该始终读作 while(fin >> InputNum) { ... },因为这将在失败时立即终止循环,或者至少检查流状态。

That number is too large for a long to store, so fin >> InputNum; does nothing. You should always read as while(fin >> InputNum) { ... }, as that will terminate the loop immediately on failure, or at least check the stream state.

汹涌人海 2024-12-11 04:13:27

您的平台上的 long 类型似乎是 32 位宽。数字 2147483648 (0x80000000) 太大,无法表示为有符号 32 位整数。您要么需要无符号类型(显然不适用于负数),要么需要 64 位整数。

另外,您应该检查读取是否成功:

  ...
  cout << endl;
  if (!(fin >> InputNum)) break; // break or otherwise handle the error condition
}

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:

  ...
  cout << endl;
  if (!(fin >> InputNum)) break; // break or otherwise handle the error condition
}
独木成林 2024-12-11 04:13:27

您不检查 EOF,因此永远陷入循环中。 鳍>>如果成功,InputNum 表达式返回 true,否则返回 false,因此将代码更改为类似以下内容将解决问题:

while ((fin >> InputNum) && InputNum >= 0)
{
  // ...
}

You don't check for EOF, thus being trapped in a loop forever. fin >> InputNum expression returns true if succeeded, false otherwise, so changing you code to something like this will solve the problem:

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