【问题标题】:Getline not reading from file correctly? [duplicate]Getline 没有正确读取文件? [复制]
【发布时间】:2017-11-15 17:16:47
【问题描述】:

在一个项目中,我需要用数字填充文件并使用 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;
}

【问题讨论】:

  • 您正在使用getline 忽略该行...
  • 抱歉,您能详细说明一下吗?在这里对 c++ 超级新手。我试过查找这个问题,我看过的大多数代码都使用 getline 。它应该看起来如何?

标签: c++ file getline


【解决方案1】:

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

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

其次,你写 8 个数字,读 9,然后将总数除以 7 得到平均值。那肯定坏了。

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

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

【讨论】:

    【解决方案2】:

    你的代码有很多问题。

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

    您根本没有对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;
    }
    

    数字.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

    输出.txt

    数字: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;
    }
    

    输出.txt

    数字: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

    【讨论】:

      猜你喜欢
      • 2022-12-30
      • 1970-01-01
      • 1970-01-01
      • 2022-07-02
      • 2016-02-26
      • 2019-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多