Writing Time class objects to a file. - C++ File Stream

C++ examples for File Stream:stream

Description

Writing Time class objects to a file.

Demo Code

                                                              
#include <cctype>
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using std::string;
                                                              
class Time//w  w  w  .  j  av a2 s  . c om
{
  private:
    int hours {};
    int minutes {};
    int seconds {};
                                                              
  public:
    Time() = default;
    Time(int h, int m, int s);
                                                              
    int getHours()const { return hours; }
    int getMinutes()const { return minutes; }
    int getSeconds()const { return seconds; }
                                                              
    friend std::istream& operator>> (std::istream& in, Time& rT);  // Friend extraction operator
};
                                                              
std::ostream& operator <<(std::ostream& out, const Time& rT);      // Overloaded insertion operator declaration
                                                              
// Constructor
Time::Time(int h, int m, int s)
{
  seconds = s%60;                      // Seconds left after removing minutes
  minutes = m + s/60;                  // Minutes plus minutes from seconds
  hours = h + minutes/60;              // Hours plus hours from minutes
  minutes %= 60;                       // Minutes left after removing hours
}
                                                              
// Insertion operator
std::ostream& operator <<(std::ostream& out, const Time& rT)
{
  out << ' ' << rT.getHours() << ':';
  char fillCh {out.fill('0')};                             // Set fill for leading zeros
                                                              
  out << std::setw(2) << rT.getMinutes() << ':'
      << std::setw(2) << rT.getSeconds() << ' ';
  out.fill(fillCh);                                        // Restore old fill character
  return out;
}
                                                              
// Extraction operator
std::istream& operator>> (std::istream& in, Time& rT)
{
  char ch {};                                              // Stores ':'
  in >> rT.hours >> ch >> rT.minutes >> ch >> rT.seconds;
                                                              
  // Ensure seconds and minutes less than 60
  rT.minutes += rT.seconds/60;
  rT.hours += rT.minutes/60;
  rT.minutes %= 60;
  rT.seconds %= 60;
  return in;
}
int main()
{
  string fileName;
  std::cout << "Enter the name of the file you want to write, including the path: ";
  std::getline(std::cin, fileName);
  std::ofstream outFile {fileName};
  if(!outFile)
  {
    std::cerr << "Failed to open output file. Program terminated.\n";
    exit(1);
  }
                                                              
  Time period;
  char ch {'n'};
                                                              
  do
  {
    std::cout << "Enter a time as hours:minutes:seconds, and press Enter: ";
    std::cin >> period;
    outFile << period;
    std::cout << "Do you want to enter another?(y or n): ";
    std::cin >> ch;
  }while(std::toupper(ch)=='Y');
                                                              
  outFile.close();
                                                              
  std::cout << std::endl;
}

Result


Related Tutorials