C++ Getline未正确读取文件?

C++ Getline未正确读取文件?,c++,file,getline,C++,File,Getline,在一个项目中,我需要用数字填充一个文件,并使用getline逐行读取这些数字,然后显示每行的总数、平均数、最大值和最小值。所有的东西都准备好了,除了我的getline似乎不起作用,因为总计、平均值等的输出总是0。任何帮助都将不胜感激。谢谢 #include <iostream> #include <fstream> #include <string> using namespace std; int main() { ofstream myfile;

在一个项目中,我需要用数字填充一个文件,并使用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;
}
#包括
#包括
#包括
使用名称空间std;
int main(){
流文件;
myfile.open(“file.txt”);
如果(!myfile)
{
cout>e>>f>>g>>h>>i;
总计=a+b+c+d+e+f+g+h+i;
平均数=总数/7;
而(填充>>num)
{
max=num;
min=num;
如果(最大数量)
{
min=num;
}
}

我的文件这里发生的各种古怪的事情

首先打开一个文件进行写入,然后打开它进行读取,然后在读取时对其进行写入可能会起作用,但看起来很奇怪。最好在第二阶段写出一个新文件

第二,你写8个数字,读9,然后把总数除以7得到平均值。这肯定是错误的

这种风格很奇怪,使用一个while循环,然后在该循环中使用另一个while循环运行到文件末尾。这真的是你的意思吗?while on min和max将读取整个文件


在“最大值”和“最小值”中,应将“最小值”设置为一个非常大的数字,而不是0。否则,“最小值>数值”将永远不会为真

在打开
infle
之前,您没有关闭
myfile
(应该是
std::ifstream
)。虽然严格来说这不是一个错误,但这是不正常的。如果在读取文件时试图修改该文件,这是不安全的。请先写入新文件,然后在完成后用新文件替换旧文件(如果需要)

您根本没有对
infle
执行任何错误处理

你是

你对
infle
的阅读太离谱了,毫无意义。它需要完全重写

每行输出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;
}
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: 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 数量:总平均最大最小值 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
你使用<代码> GETLION>代码来忽略这一行…抱歉,你能详细说明吗?这里是C++的超级新版本。我试过看这个问题,我看过的大多数代码都是这样的。
#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;
}
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