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

标签 c++

如果我们有一个存储日期的文件,后面跟着 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;

    }


}

最佳答案

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

calculate the average of these numbers and put the output in another file

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

the output file must have 10 lines of dates followed by the average value.

你没写日期


一种简单的方法是逐行读取输入文件行,然后使用 stringstream 逐个提取日期和值

来自您的代码的建议:

#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

关于c++ - 如何从 C++ 文件中读取特定数量的字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55393597/

相关文章:

c++ - 在 C++ 的链接列表方法中,如何将参数默认为其最后一个索引?

C++:我可以使用带有新位置的 vector 吗?

c++ - 整数和实数类型的不同模板行为

java - 找到多组对象的最佳匹配的算法

c++ - boolean 递归函数总是返回真

c++ - 使用 dlopen/libdl.co -ldl 标志时出现 CLion IDE 问题

c++ - 如何在 Eclipse 中配置 Crypto++?

c++ - SAPI:应用程序不说话

c++ - 在 Opengl ES 2.0 VBO 中使用 Eigen::Vector3f

c++ - 显式 void 指针作为函数参数