E
E
evg_962018-04-29 18:10:43
C++ / C#
evg_96, 2018-04-29 18:10:43

How to properly organize file input?

The book has a code example demonstrating file input, but unfortunately what is described in the code does not correspond to the result described in the same book.
In general, there is this code:

code
#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <fstream>
#include <cstdlib>

const int size = 60;

int main()
{
    char filename[size];
    std::ifstream in;

    std::cout << "Enter name of data file: ";

    std::cin.getline(filename, size);

    in.open(filename);

    if (!in.is_open())
    {
        std::cout << "Could not open the file " << filename << std::endl;
        std::cout << "Program terminating.\n";

        exit(EXIT_FAILURE);
    }

    double value;
    double sum = 0.0;
    int count = 0;

    in >> value;
    count++;
    sum += value;

    std::cout << "value: " << value << ", sum: " << sum << std::endl;

    while (in.good())
    {
        in >> value;
        count++;
        sum += value;

        std::cout << "value: " << value << ", sum: " << sum << std::endl;
    }

    if (in.eof())
        std::cout << "End of file reached.\n";
    else if (in.fail())
        std::cout << "Input terminated by data mismatch.\n";
    else
        std::cout << "Input terminated for unknown reason.\n";

    if (count == 0)
        std::cout << "No data processed.\n";
    else
    {
        std::cout << "Items read: " << count << std::endl;
        std::cout << "Sum: " << sum << std::endl;
        std::cout << "Average: " << sum / count << std::endl;
    }

    in.close();

    system("PAUSE");

    return 0;
}


Initially, the code example contained the following piece of code:
in >> value;

while (in.good())
{
    ++count;
    sum += value;
    
    in >> value;
}

But no matter how it does not correspond to the logical result. As a result, it turned out that the last element was not taken into account.
In general, how to properly organize file input, so that everything is beautiful and all data is taken into account, and not how I piled up the same code twice to stupidly fit the result ...?
code2
in >> value;
count++;
sum += value;

std::cout << "value: " << value << ", sum: " << sum << std::endl;

while (in.good())
{
    in >> value;
    count++;
    sum += value;

    std::cout << "value: " << value << ", sum: " << sum << std::endl;
}

Correct:
5ae5e1590b3ed970319138.pngIncorrect:
5ae5e1c2bb7af789551173.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
NIKITF, 2021-08-08
@NIKITF

Hello.
I offer my own solution based on the string class

#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
  string namefile; cout << "Enter name of file: "; 
  cin >> namefile;
  namefile += ".txt"; cout << endl;
  ifstream Q(namefile); string temporary; 
  double summary{ 0 }; unsigned items{ 0 };
  while (Q >> temporary)
  {
    summary += stod(temporary);
    items++;
    cout << "Value: " << stod(temporary);
    cout << endl<< "Current summary: " << summary << endl;
    cout << endl<<endl<<endl; temporary.clear();
  }
  cout << "We have reached the edge." << endl << endl;
  cout << items << " items have just read" << endl << endl;
  cout << "Summary: " << summary << endl << endl;
  cout << "Average: " << (summary / items);
  return 0;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question