创建文件并阅读文件

时间:2016-04-05 21:27:43

标签: c++

我需要从文本文件中给出的数字列表中计算数字,平均值,总和,最高和最低数量。我得到了它的工作,但由于某种原因给了我文件中的数字列表然后它说最高和最低= 0我无法找到帮助。

文件中的数字是

8 50 74 59 31 73 45 79 24 10 41 66 93 43 88 4 28 三十 41 13 4 70 10 58 61

代码:

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

int main ()
{
    string STRING;
    ifstream infile;
    infile.open ("Numbers.txt");
    while(!infile.eof()) // To get you all the lines.
    {
        getline(infile,STRING); // Saves the line in STRING.
        cout<<STRING; // Prints our STRING.
    }

    int count = 0;
    float sum = 0;
    float num = 0;
    float solution = 0;

    infile.open("Numbers.txt");

    if (infile)
        infile>>num;

    while (infile && count <25)
    {
        sum=sum+num;
        count++;
        infile>>num;
    }

    if (count <0)
    {
        solution=sum/count;

        cout <<"Number of numbers is:"<<number<<endl;
        cout<<"sum of all number is:"<<sum<<endl;
        cout<<"The average of all numbers in the file is "
        <<solution<<endl;
    }

    int highest;
    int lowest;

    while(infile >> highest)
        highest = count;
     while(infile >> lowest )
         lowest = count;
    cout<<" Highest is:"<<highest<<endl;
    cout <<"Lowest is:"<<lowest<<endl;

    infile.close();


}

3 个答案:

答案 0 :(得分:0)

删除 如果(infile)     infile&gt;&gt; NUM; 在while之前的行(infile&amp;&amp;&lt; 25)循环。

答案 1 :(得分:0)

第二次从文件中读取列表时,读取光标仍位于文件末尾。 它需要事先重置到文件的开头。尝试在文件已经打开时打开它不是解决方案。

您可以简单地使用搜索(但请记住清除流的状态!):

infile.clear();
infile.seekg(0, ios::beg);

注意:在尝试直接阅读整个列表之前,您可能不想单独阅读第一个号码。

答案 2 :(得分:0)

我的代码中有一些我不理解的东西(比如重复阅读文件)和其他完全错误的东西,比如你试图从文件中提取最高和最低数字的部分或使用number代替count。这是一个固定版本(假设符合C ++ 11的编译器):

#include <iostream>
#include <fstream>
#include <limits>   // for max and min float

using std::cout;

int main ()
{
    // open file
    std::ifstream infile("Numbers.txt");
    if ( !infile.good() )
    {
        cout << "Error, can't open number's file.\n";
        return -1;
    }

    // variables initialization  
    int count = 0;
    float num,
          min = std::numeric_limits<float>::max(),      // to be sure to update it
          max = std::numeric_limits<float>::lowest(),
          sum = 0.0,
          average = 0.0;
    // read all numbers till EOF or invalid input
    while( infile >> num )
    {
        // output the numbers in the same loop
        cout << num << ' ';
        sum += num;
        if ( num < min )
            min = num;
        if ( num > max )
            max = num;
        ++count;
    }

    if (count > 0)
    {
        cout << "\nThere are " << count << " numbers in the file"
             << "\nThe sum of all number is: " << sum
             << "\nThe average of all numbers in the file is: " << sum / count
             << "\nThe lowest of all numbers is: " << min
             << "\nThe highest of all numbers is: " << max << '\n';
    }
    else
    {
        cout << "No number read from file";
    }

    return 0;
}

根据您发布的数据,该程序的输出为:

8 50 74 59 31 73 45 79 24 10 41 66 93 43 88 4 28 30 41 13 4 70 10 58 61 
There are 25 numbers in the file
The sum of all number is: 1103
The average of all numbers in the file is: 44.12
The lowest of all numbers is: 4
The highest of all numbers is: 93