如何从c ++文件中读取特定数量的字符?

问题描述 投票:0回答:2

如果我们有一个存储日期并因此后跟24个数字的文件,我们需要做的是计算这些数字的平均值并将输出放在另一个文件中。此外,文件中的这些数字是char形式而不是int。我无法弄清楚如何阅读每个数字并添加它。说该文件包含10个这样的行。那么输出文件必须有10行日期后跟平均值。

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

int main()
{
    fstream objtread,objtwrite;
    objtread.open("temperature.txt",ios::in);
    objtwrite.open("average.txt",ios::out);
    char temp[10];
    float temperature;


    while(objtread)
    {   float sum=0.0;

        objtwrite<<getline(temp,10);
        while()
        sum = sum + get(temperature);
        objtwrite<<setprecision(3)<<sum;

    }


}
c++
2个回答
3
投票

你的代码无法编译,例如while()无效且get()未定义

计算这些数字的平均值并将输出放在另一个文件中

目前你只计算总和,你错过了除以平均值

输出文件必须有10行日期,后跟平均值。

你错过了写日期


一种简单的方法是每行读取输入文件行,然后使用字符串流提取日期和每个值之后

您的代码中的提案:

#include <iostream>
#include<fstream>
#include<iomanip>
#include<string>
#include <sstream> 

using namespace std;

int main()
{
  fstream objtread,objtwrite;

  objtread.open("temperature.txt",ios::in);
  objtwrite.open("average.txt",ios::out);

  if (!objtread.is_open())
    cerr << "cannot open temperature.txt" << endl;
  else if (!objtwrite.is_open())
    cerr << "cannot open average.txt" << endl;
  else {
    string line;

    while (line.clear(), getline(objtread, line), line.length() > 10) {
      string date = line.substr(0, 10);
      stringstream ss(line.substr(10));
      double sum = 0, value;
      int n = 0;

      while (ss >> value) {
        sum += value;
        n += 1;
      }

      objtwrite << date << ' ' << setprecision(3) << ((n == 0) ? 0.0 : sum/n) << endl;
    }
  }
}

编译和执行:

/tmp % g++ -pedantic -Wextra -Wall a.cc
/tmp % cat temperature.txt 
2019/02/28 20.44 20.25 20.14 20.02 19.87 19.66 19.41 19.13 19.51 20.44 21.82 22.92 23.49 23.60 22.72 22.54 22.28 21.62 20.34 19.05 20.39 19.72 19.11 19.12
2019/03/01 19.12 19.12 19.12 19.00 19.04 19.14 19.55 19.77 20.06 20.16 20.77 21.03 21.09 21.53 21.44 21.15 20.79 20.26 19.76 19.43 20.55 20.39 20.26 20.21
2019/03/02 20.13 19.91 19.63 19.68 19.81 20.07 20.41 20.16 20.99 21.91 22.47 23.36 23.26 23.27 23.20 23.20 22.80 21.95 21.34 21.18 21.07 21.16 21.15 21.14
2019/03/03 21.07 20.97 20.92 20.95 20.97 20.98 20.98 21.15 21.45 22.00 22.37 23.28 23.31 23.05 23.18 23.06 22.94 22.66 21.64 20.67 21.00 20.47 20.07 19.75
2019/03/04 19.41 19.02 18.76 19.16 18.99 18.90 18.80 18.86 19.50 20.57 20.87 21.00 21.94 22.81 23.11 23.10 22.01 22.32 21.84 21.63 21.52 21.57 21.60 21.61
2019/03/05 21.57 21.49 21.44 21.37 21.34 21.44 21.53 21.85 22.54 23.08 23.49 23.54 23.67 24.30 24.39 25.75 24.11 23.82 23.27 22.83 21.78 21.79 21.79 21.76
2019/03/06 21.72 21.72 21.75 21.93 22.09 22.11 21.68 21.09 21.62 21.81 22.35 22.25 22.72 23.34 23.21 23.49 23.19 22.73 22.43 22.01 18.50 17.11 16.95 16.70
2019/03/07 16.62 16.48 16.44 16.43 16.53 16.87 17.23 17.71 18.15 18.47 19.02 19.39 19.62 19.86 19.96 19.66 19.25 18.82 18.36 17.97 16.84 17.00 17.17 17.22
2019/03/08 17.17 16.78 16.29 15.73 15.38 15.38 15.53 15.78 16.15 16.66 17.29 17.95 18.38 18.63 18.83 18.96 19.05 19.08 19.10 19.14 18.36 18.63 18.89 19.14
2019/03/09 19.34 19.56 19.76 19.91 20.04 20.19 20.30 20.42 20.69 21.06 21.72 22.17 22.61 23.37 23.66 23.44 23.31 23.07 22.76 22.41 16.59 16.72 16.81 16.87
2019/03/10 16.86 16.88 16.85 16.70 16.58 16.45 16.31 16.10 16.20 16.44 16.75 17.07 17.21 17.19 17.27 17.05 16.84 16.75 16.69 16.59 16.53 16.43 16.14 15.98
2019/03/11 16.07 16.17 16.31 16.42 16.46 16.48 16.47 16.54 17.30 18.37 19.64 20.53 21.11 21.42 21.50 21.35 20.68 20.37 19.87 19.50 17.63 17.39 17.25 17.79
2019/03/12 18.37 18.61 18.39 17.70 17.40 17.99 17.14 17.20 19.31 20.27 20.94 21.80 22.30 22.54 22.58 22.45 22.15 21.42 20.37 19.32 19.61 19.29 19.01 18.79
2019/03/13 18.56 18.36 18.23 18.14 18.11 18.04 17.89 18.32 20.07 22.39 23.13 23.71 23.98 23.96 23.89 23.67 23.38 22.94 22.08 21.66 21.53 21.46 21.37 21.24
/tmp % ./a.out
/tmp % cat average.txt
2019/02/28 20.7
2019/03/01 20.1
2019/03/02 21.4
2019/03/03 21.6
2019/03/04 20.8
2019/03/05 22.7
2019/03/06 21.4
2019/03/07 18
2019/03/08 17.6
2019/03/09 20.7
2019/03/10 16.7
2019/03/11 18.4
2019/03/12 19.8
2019/03/13 21.1

0
投票

稍微不同的方法是将每个数字存储在std::vector中,然后使用std::accumulate除以该矢量vector::size()的大小来获得平均值。

int main()
{
    std::ifstream obj_read("in.txt");

    if( !obj_read.is_open() )
        return EXIT_FAILURE;

    std::string line;
    while (std::getline(obj_read, line))
    {
        std::cout<< line << " = ";

        line.erase(line.begin(),line.begin()+line.find(" ")+1);

        std::stringstream s(line);
        std::vector<double> numbers;
        double v=0.0;

        while (s >> v)
            numbers.push_back(v);

        auto average = std::accumulate(std::begin(numbers),std::end(numbers),0.0) / numbers.size();
        std::cout<< average <<std::endl;
    }

    obj_read.close();
    return EXIT_SUCCESS;
}

结果是:

2019/02/28 20.44 20.25 20.14 20.02 19.87 19.66 19.41 19.13 19.51 20.44 21.82 22.92 23.49 23.60 22.72 22.54 22.28 21.62 20.34 19.05 20.39 19.72 19.11 19.12 = 20.7329
2019/03/01 19.12 19.12 19.12 19.00 19.04 19.14 19.55 19.77 20.06 20.16 20.77 21.03 21.09 21.53 21.44 21.15 20.79 20.26 19.76 19.43 20.55 20.39 20.26 20.21 = 20.1142
2019/03/02 20.13 19.91 19.63 19.68 19.81 20.07 20.41 20.16 20.99 21.91 22.47 23.36 23.26 23.27 23.20 23.20 22.80 21.95 21.34 21.18 21.07 21.16 21.15 21.14 = 21.3854
2019/03/03 21.07 20.97 20.92 20.95 20.97 20.98 20.98 21.15 21.45 22.00 22.37 23.28 23.31 23.05 23.18 23.06 22.94 22.66 21.64 20.67 21.00 20.47 20.07 19.75 = 21.6204
2019/03/04 19.41 19.02 18.76 19.16 18.99 18.90 18.80 18.86 19.50 20.57 20.87 21.00 21.94 22.81 23.11 23.10 22.01 22.32 21.84 21.63 21.52 21.57 21.60 21.61 = 20.7875
2019/03/05 21.57 21.49 21.44 21.37 21.34 21.44 21.53 21.85 22.54 23.08 23.49 23.54 23.67 24.30 24.39 25.75 24.11 23.82 23.27 22.83 21.78 21.79 21.79 21.76 = 22.6642
2019/03/06 21.72 21.72 21.75 21.93 22.09 22.11 21.68 21.09 21.62 21.81 22.35 22.25 22.72 23.34 23.21 23.49 23.19 22.73 22.43 22.01 18.50 17.11 16.95 16.70 = 21.4375
2019/03/07 16.62 16.48 16.44 16.43 16.53 16.87 17.23 17.71 18.15 18.47 19.02 19.39 19.62 19.86 19.96 19.66 19.25 18.82 18.36 17.97 16.84 17.00 17.17 17.22 = 17.9613
2019/03/08 17.17 16.78 16.29 15.73 15.38 15.38 15.53 15.78 16.15 16.66 17.29 17.95 18.38 18.63 18.83 18.96 19.05 19.08 19.10 19.14 18.36 18.63 18.89 19.14 = 17.595
2019/03/09 19.34 19.56 19.76 19.91 20.04 20.19 20.30 20.42 20.69 21.06 21.72 22.17 22.61 23.37 23.66 23.44 23.31 23.07 22.76 22.41 16.59 16.72 16.81 16.87 = 20.6992
2019/03/10 16.86 16.88 16.85 16.70 16.58 16.45 16.31 16.10 16.20 16.44 16.75 17.07 17.21 17.19 17.27 17.05 16.84 16.75 16.69 16.59 16.53 16.43 16.14 15.98 = 16.6608
2019/03/11 16.07 16.17 16.31 16.42 16.46 16.48 16.47 16.54 17.30 18.37 19.64 20.53 21.11 21.42 21.50 21.35 20.68 20.37 19.87 19.50 17.63 17.39 17.25 17.79 = 18.4425
2019/03/12 18.37 18.61 18.39 17.70 17.40 17.99 17.14 17.20 19.31 20.27 20.94 21.80 22.30 22.54 22.58 22.45 22.15 21.42 20.37 19.32 19.61 19.29 19.01 18.79 = 19.7896
2019/03/13 18.56 18.36 18.23 18.14 18.11 18.04 17.89 18.32 20.07 22.39 23.13 23.71 23.98 23.96 23.89 23.67 23.38 22.94 22.08 21.66 21.53 21.46 21.37 21.24 = 21.0879
© www.soinside.com 2019 - 2024. All rights reserved.