samedi 18 février 2023

How to read, write parent class & Inherited class's data to a binary file in C++ altogether?

I am working on a routine management system. I just thought like this, the binary file will have 8 lines, 1st line for password matching (8 characters fixed, drama for college), rest of the lines will have the timetable for each day and each line no corresponds to the day no.

Now I made 1 TimeTable Class and a Inherited class Period. Timetable holds day no and Period holds all the details of a period like faculty id, time, sub code, etc.

Now, problem is, how can I write & read the whole data to & from the binary file?
Also, how can I display the whole routine like spreadsheet?

table_manage.h

#define pause()                               \
    do                                        \
    {                                         \
        cout << "\n";                         \
    } while (cin.get() != '\n');              \
    do                                        \
    {                                         \
        cout << "Press a key to continue..."; \
    } while (cin.get() != '\n')

class TimeTable
{
public:
    int getDay();
    void setDay(int day);
    void displayRoutine(std::string uname);

private:
    int day = 0;
};

class Period : public TimeTable
{
public:
    void periodConstructor(int periodNo, int startTime, int endTime, std::string facultyId, std::string subId, int day, int totalPeriods)
    {
        this->periodNo = periodNo;
        this->startTime = startTime;
        this->endTime = endTime;
        this->facultyId = facultyId;
        this->subId = subId;
    }
    void openFileChk(std::string uname);

private:
    int periodNo;
    int startTime;
    int endTime;
    std::string facultyId;
    std::string subId;
};

table_manage.cpp

#include <iostream>
#include <cstdlib>
#include <vector>
#include <algorithm>
#include <cmath>
#include <array>
#include <set>
#include <map>
#include <cstring>
#include <queue>
#include <stack>
#include <chrono>
#include <random>
#include <functional>
#include <limits>
#include <fstream>
#include <sstream>
#include <filesystem>

#include "login.h"
#include "signup.h"
#include "table_manage.h"

using namespace std;

int TimeTable::getDay()
{
    return day;
}

void TimeTable::setDay(int day)
{
    this->day = day;
}

void Period::openFileChk(string uname)
{
    string funame = uname + ".dat";
    if (std::filesystem::exists(funame))
    {
        ifstream file(funame, ios::out | ios::binary);
        file.close();
    }
}

void TimeTable::displayRoutine(std::string uname)
{
    string funame = uname + ".dat";
    if (std::filesystem::exists(funame))
    {
        TimeTable period[7];
        ifstream file(funame, ios::out | ios::binary);
        file.seekg(9, ios::beg);
        for (int i = 0; i < 8; i++)
        {
            if (!file.eof())
            {
                file.read((char *)&period[i], sizeof(Period));
                setDay(i + 1);
            }
        }

        file.close();
    }
}

I separated the modules as I have also made login & signup features (module) in my code.

To view the full project you can check my GitHub Repo: https://github.com/ArchismanKarmakar/TimeTable-AK

I am working on a routine management system. I just thought like this, the binary file will have 8 lines, 1st line for password matching (8 characters fixed, drama for college), rest of the lines will have the timetable for each day and each line no corresponds to the day no.

Now I made 1 TimeTable Class and a Inherited class Period. Timetable holds day no and Period holds all the details of a period like faculty id, time, sub code, etc.

Now, problem is, how can I write & read the whole data to & from the binary file?
Also, how can I display the whole routine like spreadsheet?

Aucun commentaire:

Enregistrer un commentaire