首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当从文件中读取大量数据时,如何获得准确的输出?

问当从文件中读取大量数据时,如何获得准确的输出?
EN

Stack Overflow用户
提问于 2021-10-18 02:13:08
回答 2查看 141关注 0票数 0

我试图使用for循环从文件中读取10万个int值。我也想把它们加起来,找一分钟,找一个最大值。现在,我的代码只有在我将读取值从100,000减少到仅仅100的情况下才能正确读取。即使是200个值,我的代码也只是跳过数据,没有给出正确的输出。有人能看到我的代码在读取更大数量的值时可能出错吗?谢谢!

代码语言:javascript
复制
#include <cmath>
#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

int main()
{
    int number;
    int sum;
    int big, small = 0;
    string file;

    cout << "Enter a file name: ";
    cin >> file;
    ifstream inFile(file);

    for (long i = 0; i < 100000; i++)
    {
        if (!(inFile >> number))
        {
            cout << "ERROR!";
            return 1;
        }
    }

    for (long i = 0; i < 100000; i++)
    {
        inFile >> number;
        sum = sum + number;
        if (number < small)
        {
            small = number;
        }
        if (number > big)
        {
            big = number;
        }
    }
    cout << sum << endl;
    cout << big << endl;
    cout << small << endl;
    return 0;
}

另外,我的输入文件也是这样排列的

代码语言:javascript
复制
1 2 3 4 5 6 7 8 9 0
1 2 3 4 5 6 7 8 9 0
...
...
...

当然还有其他数字

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-10-18 04:18:55

正如其他人所指出的,您的第一个循环是读取和丢弃整数,然后您的第二个循环选择了第一个循环结束的位置,而不是再次从文件的开头开始。您应该只使用一个循环。

在进入递增变量的循环之前,也不初始化sum和big变量。

尝试更像这样的东西:

代码语言:javascript
复制
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    int number, sum, big, small;
    string file;

    cout << "Enter a file name: ";
    cin >> file;
    ifstream inFile(file);

    if (!(inFile >> number))
    {
        cout << "ERROR!";
        return 1;
    }

    sum = small = big = number;

    for (int i = 1; i < 100000; ++i)
    {
        if (!(inFile >> number))
        {
            cout << "ERROR!";
            return 1;
        }

        sum += number;

        if (number < small)
        {
            small = number;
        }

        if (number > big)
        {
            big = number;
        }
    }

    cout << sum << endl;
    cout << big << endl;
    cout << small << endl;

    return 0;
}
票数 1
EN

Stack Overflow用户

发布于 2021-10-18 03:04:18

编辑注释

正如@RemyLebeau所指出的,我犯了没有初始化big或sum的错误,我对a, b, c = 0没有初始化a或b而是只初始化c这一典型错误感到非常愚蠢。

第二,正如雷米所指出的,我没有意识到流有operator!,以检查流上是否发生了故障。

考虑到这一点,下面是我的更新版本,以及后人的原始版本:

代码语言:javascript
复制
#include <cmath>
#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

int main()
{
    int number;
    int sum = 0, big = 0, small = 0;
    string file;

    cout << "Enter a file name: ";
    cin >> file;
    ifstream inFile(file);

    for (long i = 0; i < 100000; i++)
    {
        if (!(inFile >> number))
        {
            cout < "ERROR!";
            return 1;
        }
        sum = sum + number;
        if (number < small)
        {
            small = number;
        }
        if (number > big)
        {
            big = number;
        }
    }
    cout << sum << endl;
    cout << big << endl;
    cout << small << endl;
    return 0;
}

原始员额

首先,应该将sum初始化为零,因为它的值总是由自身添加的结果决定的。

代码语言:javascript
复制
int sum = 0;

其次,在您的第一个循环中,您将从文件中读取到一个变量,然后丢弃该变量的值。此循环具有推进文件指针的副作用,因此如果在第一个循环中读取100000个值,则第二个循环将从文件中的第100001个值开始。

我看不出有什么理由在两个单独的循环中这样做,我也不明白为什么要检查inFile >> number的“结果”,因为它的返回值始终不是null:

代码语言:javascript
复制
#include <cmath>
#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

int main()
{
    int number;
    int sum, big, small = 0;
    string file;

    cout << "Enter a file name: ";
    cin >> file;
    ifstream inFile(file);

    for (long i = 0; i < 100000; i++)
    {
        inFile >> number;
        sum = sum + number;
        if (number < small)
        {
            small = number;
        }
        if (number > big)
        {
            big = number;
        }
    }
    cout << sum << endl;
    cout << big << endl;
    cout << small << endl;
    return 0;
}
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69610082

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档