Getline无法正确读取文件?

时间:2017-11-15 17:16:48

标签: c++ file getline

处理我需要用数字填充文件的项目,并使用getline逐行读取这些数字,然后显示每行的总数,平均值,最大值和最小值。除了我的getline之外的所有东西似乎都不起作用,因为总数,平均值等的输出总是为0。任何帮助都是值得赞赏的。谢谢!

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() {
    ofstream myfile;
    myfile.open("file.txt");
    if(!myfile)
    {
        cout << "Unable to open file\n";
        return 0;
    }
    if(myfile.is_open())
    {
        myfile << "Numbers: \n";
        myfile << "90 63 84 52 21 93 77 46\n";
        myfile << "90 22 26 34 39 44 75 98\n";
        myfile << "28 28 85 57 28 33 66 100\n";
        myfile << "16 80 74 62 42 84 42 56\n";
        myfile << "85 44 76 97 16 64 80 14\n";
        myfile << "41 85 13 88 78 8 18 38\n";
        myfile << "53 49 71 79 75 57 93 62\n";

        fstream infile;
        infile.open("file.txt");
        int total, average, max=0, min=0, num;
        while(!infile.eof())
        {
            string line;
            getline(infile, line);
            int a, b, c, d, e, f, g, h, i;
            infile >> a >> b >> c >> d >> e >> f >> g >> h >> i;
            total = a+b+c+d+e+f+g+h+i;
            average = total/7;
            while(infile>>num)
            {
                max = num;
                min = num;
                if(max<num)
                {
                    max = num;
                }
                if(min > num)
                {
                    min = num;
                }
            }
            myfile << "                               TOTAL        AVERAGE      MAX      MIN\n";
            myfile << "90 63 84 52 21 93 77 46    " << total << "  " << average << "  " << max << "  " << min << endl;
            myfile << "90 22 26 34 39 44 75 98\n";
            myfile << "28 28 85 57 28 33 66 100\n";
            myfile << "16 80 74 62 42 84 42 56\n";
            myfile << "85 44 76 97 16 64 80 14\n";
            myfile << "41 85 13 88 78 8 18 38\n";
            myfile << "53 49 71 79 75 57 93 62\n";
        }
        infile.close();
    }
    myfile.close();
    return 0;
}

2 个答案:

答案 0 :(得分:0)

这里发生了各种各样的时髦事情

首先打开一个文件进行书写,然后打开它进行读取和写入,同时阅读可能有效,但看起来很奇怪。最好在第二阶段写出一个新文件

其次,你写了8个数字,读取9并将总数除以7得到均值。肯定会破碎。

样式很奇怪,使用while循环,然后在循环内部运行到另一个while循环的文件末尾。这真的是你的意思吗?在min和max时,将读取整个文件

在你的最大值和最小值中,你应该将min设置为一个非常大的数字,而不是0.其他明智的min&gt; num永远不会是真的

答案 1 :(得分:0)

您的代码存在许多问题。

在打开myfile之前,您没有关闭infile(而应该是std::ifstream)。虽然不是严格意义上的错误,但却很不寻常。如果您在阅读文件时尝试修改文件,则不安全。首先写入新文件,然后在完成时将旧文件替换为新文件(如果需要)。

您根本没有对infile进行任何错误处理。

你是misusing eof()

你对infile的阅读只是一蹴而就,毫无意义。它需要完全重写。

您每行输出8个数字,但每行读取9个数字,平均每个数字7个。

认为您要做的是生成一个输出文件,您可以在其中显示每行数字的统计信息。如果是这样,请尝试更像这样的事情:

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <iomanip>
using namespace std;

int main()
{
    ofstream myfile("numbers.txt");
    if (!myfile)
    {
        cout << "Unable to create numbers file\n";
        return 0;
    }

    myfile << "90 63 84 52 21 93 77 46\n";
    myfile << "90 22 26 34 39 44 75 98\n";
    myfile << "28 28 85 57 28 33 66 100\n";
    myfile << "16 80 74 62 42 84 42 56\n";
    myfile << "85 44 76 97 16 64 80 14\n";
    myfile << "41 85 13 88 78 8 18 38\n";
    myfile << "53 49 71 79 75 57 93 62\n";
    myfile.close();

    ifstream infile("numbers.txt");
    if (!infile)
    {
        cout << "Unable to open numbers file\n";
        return 0;
    }

    myfile.open("output.txt");
    if (!myfile)
    {
        cout << "Unable to create output file\n";
        return 0;
    }

    myfile << "Numbers:                       TOTAL        AVERAGE      MAX      MIN\n";

    string line;
    while (getline(infile, line))
    {
        istringstream iss(line);
        ostringstream oss;

        int num, total = 0, count = 0, average = 0, max = 0, min = 0;

        if (iss >> num)
        {
            max = min = num;

            do
            {
                total += num;
                ++count;

                if (num > max)
                    max = num;

                if (num < min)
                    min = num;

                oss << num << ' ';
            }
            while (iss >> num);

            average = total / count;
        }

        myfile << left << setw(30) << setfill(' ') << oss.str() << " ";
        myfile << left << setw(12) << setfill(' ') << total << " ";
        myfile << left << setw(12) << setfill(' ') << average << " ";
        myfile << left << setw(8) << setfill(' ') << max << " ";
        myfile << min << endl;
    }

    infile.close();
    myfile.close();

    return 0;
}

numbers.txt

90 63 84 52 21 93 77 46
90 22 26 34 39 44 75 98
28 28 85 57 28 33 66 100
16 80 74 62 42 84 42 56
85 44 76 97 16 64 80 14
41 85 13 88 78 8 18 38
53 49 71 79 75 57 93 62

output.txt的

Numbers:                       TOTAL        AVERAGE      MAX      MIN
90 63 84 52 21 93 77 46        526          65           93       21
90 22 26 34 39 44 75 98        428          53           98       22
28 28 85 57 28 33 66 100       425          53           100      28
16 80 74 62 42 84 42 56        456          57           84       16
85 44 76 97 16 64 80 14        476          59           97       14
41 85 13 88 78 8 18 38         369          46           88       8
53 49 71 79 75 57 93 62        539          67           93       49

或者,如果您使用内存数组而不是文件,则可以完全删除numbers.txt

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <iomanip>
using namespace std;

const int numbers[7][8] = {
    {90, 63, 84, 52, 21, 93, 77, 46},
    {90, 22, 26, 34, 39, 44, 75, 98},
    {28, 28, 85, 57, 28, 33, 66, 100},
    {16, 80, 74, 62, 42, 84, 42, 56},
    {85, 44, 76, 97, 16, 64, 80, 14},
    {41, 85, 13, 88, 78, 8 , 18, 38},
    {53, 49, 71, 79, 75, 57, 93, 62}
};

int main()
{
    ofstream myfile("output.txt");
    if (!myfile)
    {
        cout << "Unable to create output file\n";
        return 0;
    }

    myfile << "Numbers:                       TOTAL        AVERAGE      MAX      MIN\n";

    for(int i = 0; i < 7; ++i)
    {
        int num = numbers[i][0];
        int total = num, max = num, min = num;

        ostringstream oss;
        oss << num << ' ';

        for(int j = 1; j < 8; ++j)
        {
            num = numbers[i][j];
            total += num;

            if (max < num)
                max = num;

            if (min > num)
                min = num;

            oss << num << ' ';
        }

        int average = total / 8;

        myfile << left << setw(30) << setfill(' ') << oss.str() << " ";
        myfile << left << setw(12) << setfill(' ') << total << " ";
        myfile << left << setw(12) << setfill(' ') << average << " ";
        myfile << left << setw(8) << setfill(' ') << max << " ";
        myfile << min << endl;
    }

    myfile.close();

    return 0;
}

output.txt的

Numbers:                       TOTAL        AVERAGE      MAX      MIN
90 63 84 52 21 93 77 46        526          65           93       21
90 22 26 34 39 44 75 98        428          53           98       22
28 28 85 57 28 33 66 100       425          53           100      28
16 80 74 62 42 84 42 56        456          57           84       16
85 44 76 97 16 64 80 14        476          59           97       14
41 85 13 88 78 8 18 38         369          46           88       8
53 49 71 79 75 57 93 62        539          67           93       49