简体   繁体   中英

How to read specific number of characters from a file in c++?

if we have a file that stores the date and consequently followed by 24 numbers, what we need to do is to calculate the average of these numbers and put the output in another file. Also these numbers in the file are in char form and not int. I am unable to figure out how to read each number and add it. say the file consists of 10 such lines. then the output file must have 10 lines of dates followed by the average value.

#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;

    }


}

your code cannot compile, for instance while() is invalid and get() is not defined

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

currently you only compute the sum, you missed to divide to have the average

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

you missed to write the date


A simple way is to read the input file line per line then extract the date and after the values one per one using a stringstream

A proposal from your code :

#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;
    }
  }
}

Compilation and execution :

/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

A slightly different approach would be to store each number in a std::vector and then use std::accumulate divided by the size of that vector vector::size() to get your average.

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;
}

The result is:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM