void showMinutestoExecution(Record * myRecords , int amountRecords){
    time_t currentS , nowS = time(0);
    for(int m = 0; m < amountRecords; m++){
        currentS = myRecords[m].timeS;
        if(currentS > nowS){
            cout <<"Remain about: " <<(currentS - nowS)/60 <<" minute(s)"<< endl;
            cout << myRecords[m].mode;
            cout << myRecords[m].temperature;
            cout << endl;
        }
    }
}
void showRecordsbyDate(Record * myRecords , int amountRecords){
    string conditionalDate , currentDate;
        cout << "NOW YOU ARE IN THE |FILTER OUT BY DATE OPERATION|" << endl;
        cout << "Enter the date for filter: ";
        cin >> conditionalDate;
        cout << "Your records: " << endl;
        for(int m = 0; m < amountRecords; m++){
        currentDate = myRecords[m].date;
        currentDate.erase(0  , 5);
        currentDate.erase(10  , 7);
        if(currentDate == conditionalDate){
        cout << "Record: "<< m << endl;
        cout << "_____________________"<< endl;
        cout << myRecords[m].date;
        cout << myRecords[m].mode;
        cout << myRecords[m].temperature;
        cout << endl;
        }
    }
}
void showMyrecords(Record * myRecords , int amountRecords){
    cout << "--Current Records--" << endl;
    for(int m = 0; m < amountRecords; m++){
        cout << "Record: "<< m << endl;
        cout << "_____________________"<< endl;
        cout << myRecords[m].date;
        cout << myRecords[m].mode;
        cout << myRecords[m].temperature;
        cout << endl;
    }
    cout << "(Total): " << amountRecords <<endl;
}
void findAmountnextMon(Record* myRecords, int amountRecords){
    string conditionalMode , currentMode;
    time_t timeSmax = time(0) , timeSmin;
    int amountSatisfiedrecords = 0;
    struct tm* conditionalToday{0};
    cout << "NOW YOU ARE IN THE |FILTER OUT NEXT MONTH OPERATION|" << endl;
    cout << "Enter the mode for filter: ";
    cin >> conditionalMode;
    conditionalToday = localtime(&timeSmax);
    for(int m =0; m<2; m++){
    if(conditionalToday -> tm_mon != 11)
        conditionalToday -> tm_mon += 1;
    else{
        conditionalToday -> tm_mon = 0;
        conditionalToday -> tm_year += 1;
    }
    conditionalToday -> tm_min = 0;
    conditionalToday -> tm_hour = 0;
    conditionalToday -> tm_mday = 1;
    conditionalToday -> tm_sec = 0;
    if(m == 0)
    timeSmin = mktime(conditionalToday);
    if(m == 1)
    timeSmax = mktime(conditionalToday);
    }
    cout << "Your records: " << endl;
    for(int m = 0; m < amountRecords; m++){
        currentMode = myRecords[m].mode;
        currentMode.erase(0,5);
        currentMode.erase(currentMode.size() - 1,1);
        if(timeSmin < myRecords[m].timeS && timeSmax > myRecords[m].timeS && conditionalMode == currentMode){
        cout << "Record: "<< m << endl;
        cout << "_____________________"<< endl;
        cout << myRecords[m].date;
        cout << myRecords[m].mode;
        cout << myRecords[m].temperature;
        cout << endl;
            amountSatisfiedrecords += 1;
        }
    }
    cout << "(Total): " << amountSatisfiedrecords << endl;;
}
void findLastToday(Record *myRecord, int amountRecords){
    time_t timeSmax = time(0) , timeNow = time(0), currentS, maxS;
    int pos;
    struct tm* conditionalToday{0};
    conditionalToday = localtime(&timeSmax);
    conditionalToday -> tm_min = 59;
    conditionalToday -> tm_hour = 23;
    conditionalToday -> tm_sec = 59;
    timeSmax = mktime(conditionalToday);
    for(int m = 0; m < amountRecords; m++){
        if(myRecord[m].timeS > timeNow && myRecord[m].timeS < timeSmax){
            currentS = myRecord[m].timeS;
            if(currentS > maxS){
                maxS = currentS;
            }
        }
    }
    for(int m = 0; m < amountRecords; m++){
    if(myRecord[m].timeS == maxS){
        cout<<"The record:" << endl;
        cout<<myRecord[m].date;
        cout<<myRecord[m].mode;
        cout<<myRecord[m].temperature;
        pos = 1;
    }
    }
    if(!pos)
    cout << "No such records" << endl;
 }
void sortByDate(Record* myRecords, int amountRecords , string myPath){
    int pos, lengthMin;
    pos = myPath.rfind('\\') + 1;
    if(pos == -1)
    pos = myPath.rfind('/') + 1;
    lengthMin = myPath.size() - pos;
    string Path = myPath, currentPath , date = myRecords[0].date, currentDate , currentMinutes;
    Path = Path.erase(pos, lengthMin);
    cout << "*The files are created in: " << Path << endl;
    date.erase(0 , 5);
    date.erase(10  , 7);
    currentPath = Path + date + ".txt";
    FILE* myFile = fopen(¤tPath[0] , "w");
    for(int m = 0; m < amountRecords; m++){
        currentDate = myRecords[m].date;
        currentDate.erase(0  , 5);
        currentDate.erase(10  , 7);
        if(currentDate == date){
            currentMinutes = myRecords[m].date;
            currentMinutes.erase(0,16);
            fputs(¤tMinutes[0], myFile);
            fputs(&myRecords[m].mode[0], myFile);
            fputs(&myRecords[m].temperature[0], myFile);
        }
        else{
            fclose(myFile);
            date = currentDate;
            currentPath = Path + date + ".txt";
            myFile = fopen(¤tPath[0] , "w");
            currentMinutes = myRecords[m].date;
            currentMinutes.erase(0,16);
            fputs(¤tMinutes[0], myFile);
            fputs(&myRecords[m].mode[0], myFile);
            fputs(&myRecords[m].temperature[0], myFile);
        }
    }
    fclose(myFile);
}//rgzHeader.h
#ifndef RGZHEADER_H_INCLUDED
#define RGZHEADER_H_INCLUDED
namespace mySpace
{
struct Record{
std::string date;
std::string mode;
std::string temperature;
time_t timeS;
};
void findAmountnextMon(Record* , int);
void showMinutestoExecution(Record*, int);
void showRecordsbyDate(Record*, int);
void showMyrecords(Record*, int);
void findLastToday(Record*, int);
void sortByDate(Record* , int , std::string);
void editRecords(Record* , int , std::string);
void sortMerge(Record * , int );
Record* addRecord(std::string, Record*, int* );
time_t to_timet(std::string);
}
#endif // RGZHEADER_H_INCLUDED#include <iostream>
#include <cstdio>
#include <string>
#include <time.h>
#include "rgzHeader.h"
using namespace std;
int main(){
    using namespace mySpace;
    int choosenOperation, amountRecords = 0;
    Record* myRecords = new Record[1];
    string myPath;
    FILE* myFile;
    cout << "Before the start enter a path to the file(Max 150 symbols): ";
    getline(cin , myPath);
    if(myFile = fopen(&myPath[0] , "r")){
        char myStr[50];
        Record *oldRef;
        Record reRecord;
        cout << "*The file successfully found*" << endl;
        while(fgets(myStr, 50 ,myFile)){
            reRecord.date = myStr;
            reRecord.timeS = to_timet(myStr);
            fgets(myStr, 50 ,myFile);
            reRecord.mode = myStr;
            fgets(myStr, 50 ,myFile);
            reRecord.temperature = myStr;
            oldRef = myRecords;
            myRecords = new Record[amountRecords + 1];
            myRecords[amountRecords] = reRecord;
            for(int m = 0; m < amountRecords; m++)
            myRecords[m] = oldRef[m];
            amountRecords += 1;
            delete[] oldRef;
        }
        sortMerge(myRecords, amountRecords);
    }
    else{
        cout << "We did not find the file" <<  endl;
        cout << "Do you want to create it?(0 - Yes, 1 - No)." << endl;
        cin >> choosenOperation;
        if(choosenOperation)
            cout << "Sorry :( Check the path." << endl;
        else{
            myFile = fopen(&myPath[0] , "w");
            cout << "*The file successfully created*" << endl;
        }
    }
    fclose(myFile);
    while(1){
        cout << "                                         *|||||*- - -*|||||*- - -*|||||*" << endl;
        cout << "                                   *|||||*                             *|||||*" << endl;
        cout << "                                   *|||||*  WELCOME TO THE SMARTHOUSE  *|||||*" << endl;
        cout << "                                   *|||||*                             *|||||*" << endl;
        cout << "                                         *|||||*- - -*|||||*- - -*|||||*" << endl;
        cout << "NOW YOU ARE IN THE |MAIN MENU|" << endl;
        cout << "Please, choose the operation you wanted to do :)" << endl;
        cout << "Enter: 0 - to add a new record \n       1 - to show the records \n       2 - to edit the records" << endl;
        cout << "       5 - exit\n       3 - to sort by dates in files\n       4 - to find the last record on today" << endl;
        cout << "       6 - to count amount records of next month\n       7 - to filter out by date\n       8 - to show minutes to execution" << endl;
        cout << "Enter: ";
        cin >> choosenOperation;
        if(choosenOperation == 0)
            myRecords = addRecord(myPath , myRecords , &amountRecords);
        else if(choosenOperation == 1)
            showMyrecords(myRecords , amountRecords);
        else if(choosenOperation == 2)
            editRecords(myRecords, amountRecords , myPath);
        else if(choosenOperation == 3)
            sortByDate(myRecords , amountRecords , myPath);
        else if(choosenOperation == 4)
            findLastToday(myRecords, amountRecords);
        else if(choosenOperation == 6)
            findAmountnextMon(myRecords, amountRecords);
        else if(choosenOperation == 7)
            showRecordsbyDate(myRecords, amountRecords);
        else if(choosenOperation == 8)
            showMinutestoExecution(myRecords , amountRecords);
        else if(choosenOperation == 5){
            cout << "THANK YOU (^-^)" << endl;
            break;
        }
    }
    delete[] myRecords;
}#include <iostream>
#include <cstdio>
#include <string>
#include <time.h>
using namespace std;
struct Record{
    string date;
    string mode;
    string temperature;
    time_t timeS;
};
time_t to_timet(string currentDate){
    string sConverter = "";
    struct tm currentTime{0};
    currentTime.tm_year = stoi(sConverter + currentDate[11] + currentDate[12]+ currentDate[13] + currentDate[14]) - 1900;
    if(currentDate[5] != '0')
    currentTime.tm_mday = stoi(sConverter + currentDate[5] + currentDate[6]);
    else
    currentTime.tm_mday = stoi(sConverter + currentDate[6]);
    if(currentDate[8] != '0')
    currentTime.tm_mon = stoi(sConverter + currentDate[8] + currentDate[9]) - 1;
    else
    currentTime.tm_mon = stoi(sConverter + currentDate[9]) - 1;
    if(currentDate[16] != '0')
    currentTime.tm_hour = stoi(sConverter + currentDate[16] + currentDate[17]);
    else
    currentTime.tm_hour = stoi(sConverter + currentDate[17]);
    if(currentDate[19] != '0')
    currentTime.tm_min = stoi(sConverter + currentDate[19] + currentDate[20]);
    else
    currentTime.tm_min = stoi(sConverter + currentDate[20]);
    return mktime(¤tTime);
}
void sortMerge(Record *myRecords , int sizeMas){
    Record timedValue;
    for(int m = 0; m < sizeMas - 1; m++){
        for(int n = 1 + m; n < sizeMas ; n++)
        if(myRecords[m].timeS > myRecords[n].timeS){
            timedValue = myRecords[m];
            myRecords[m]= myRecords[n];
            myRecords[n] = timedValue;
        }
    }
}
void reWriteFile(Record *myMas , int amountRecords , string myPath){
    FILE * myFile = fopen(&myPath[0] , "w");
    for(int m = 0; m < amountRecords; m++){
        fputs(&myMas[m].date[0], myFile);
        fputs(&myMas[m].mode[0], myFile);
        fputs(&myMas[m].temperature[0], myFile);
    }
    fclose(myFile);
}
void editRecords(Record *myRecords , int amountRecords , string myPath){
    string  fieldToedit, date , valueToexchange , sConverter;
    int choosenOperation, dateToedit;
    showMyrecords(myRecords, amountRecords);
    cout << "NOW YOU ARE IN THE |EDITING OPERATION|" << endl;
    cout << "Enter the record's number to edit the record. For saving enter -1, you will be returned in the main menu" << endl;
    while(1){
        cout << "Number to edit: ";
        cin >> dateToedit;
        if(dateToedit == -1)
            break;
                cout << "Enter the field you want to edit(mode, temperature, time, date): ";
                cin >> fieldToedit;
                cout << "Enter the value you want to exchange: ";
                cin >> valueToexchange;
                if(fieldToedit == "mode")
                    myRecords[dateToedit].mode = "Mode:" + valueToexchange + '\n';
                else if(fieldToedit == "date"){
                    myRecords[dateToedit].date = "Date(" + valueToexchange + myRecords[dateToedit].date.erase(0 , 15);
                    myRecords[dateToedit].timeS = to_timet(myRecords[dateToedit].date);
                    }
                else if(fieldToedit == "time"){
                    myRecords[dateToedit].date = myRecords[dateToedit].date.erase(16,6) + valueToexchange + '\n';
                    myRecords[dateToedit].timeS = to_timet(myRecords[dateToedit].date);
                }
                else if(fieldToedit == "temperature")
                    myRecords[dateToedit].temperature = "Temperature:" + valueToexchange + '\n';
                cout << "(Edited)" << endl;
                cout << "_____________________"<< endl;
                cout << myRecords[dateToedit].date;
                cout << myRecords[dateToedit].mode;
                cout << myRecords[dateToedit].temperature;
                cout << endl;
            }
    cout << "Do you want to sort the file after editing?(0 - No, 1 - Yes)."<< endl;
    cin >> choosenOperation;
    if(choosenOperation){
        sortMerge(myRecords,amountRecords);
        reWriteFile(myRecords , amountRecords, myPath);
    }
    else{
        reWriteFile(myRecords , amountRecords, myPath);
        sortMerge(myRecords ,amountRecords);
    }
}
Record* addRecord(string myPath, Record *myRecords , int *amountRecords){
    FILE* myFile = fopen(&myPath[0] , "a");
    int amountAdd , minPosition, minPos = 0;;
    Record currentRecord;
    Record *oldRef;
    string time;
    cout << "NOW YOU ARE IN THE |ADDING OPERATION|" << endl;
    cout << "How many records you want to add?"<<endl;
    cout << "Enter: ";
    cin >> amountAdd;
    cout << "--Example for adding--" << endl;
    cout << "Date: 02.09.2020" << endl;
    cout << "Time: 04:45" << endl;
    cout << "Mode: Heating(for ex. Freezing)" << endl;
    cout << "Temperature of oxe(C): 45.6" << endl;
    for(int m = 0; m < amountAdd; m++){
        cout << "---Your Form---" <<endl;
        cout << "Date: ";
        cin >> currentRecord.date;
        cout << "Time: ";
        cin >> time;
        currentRecord.date = "Date(" + currentRecord.date + ")" + time + '\n';
        currentRecord.timeS = to_timet(currentRecord.date);
        fputs(¤tRecord.date[0],myFile);
        cout << "Mode: ";
        cin >> currentRecord.mode;
        currentRecord.mode = "Mode:" + currentRecord.mode + '\n';
        fputs(¤tRecord.mode[0],myFile);
        cout << "Temperature of oxe(C): ";
        cin >> currentRecord.temperature;
        currentRecord.temperature = "Temperature:" + currentRecord.temperature + '\n';
        fputs(¤tRecord.temperature[0],myFile);
        for(int m = 0; m < *amountRecords; m++){
            if(currentRecord.timeS >= myRecords[m].timeS){
                if(m + 1 != *amountRecords)
                continue;
                else
                minPos = m + 1;
                }
                else{
                minPos= m;
                break;
            }
            }
            oldRef = myRecords;
            myRecords = new Record[*amountRecords + 1];
            myRecords[minPos] = currentRecord;
            for(int m = 0; m < minPos; m++)
                myRecords[m] = oldRef[m];
            for(int m = minPos; m < *amountRecords; m++)
                myRecords[m + 1] = oldRef[m];
            delete[] oldRef;
            *amountRecords += 1;
    }
    fclose(myFile);
    return myRecords;
}