I saved some data into the file and when I read all the data at the end of the program right after calling the destructor function I got an access violation error. The data is read successfully but the exception keeps throwing
This is my class:
class DoList
{
public:
DoList(const std::string& , const std::string& , const std::string& ,const int& , const int&, const int&, const int&);
DoList();
~DoList();
bool Input_Task();
bool Remove_Task();
void Read_Data();
private:
int m_Year;
int m_Month;
int m_Day;
int m_ID;
std::string m_TaskName;
std::string m_Description;
std::string m_Status;
};
This is my class.cpp
The function where I input task:
bool DoList::Input_Task() /*Works fine*/
{
DoList Data;
std::cout << "Enter Your Task ID: ";
std::cin >> Data.m_ID;
std::cin.ignore();
std::cout << "Enter Your Task Name: ";
std::getline(std::cin, Data.m_TaskName);
std::cout << "Enter Description: ";
std::getline(std::cin, Data.m_Description);
time_t now = time(NULL);
tm now_tm = {};
localtime_s(&now_tm, &now);
Data.m_Year = 1900 + now_tm.tm_year;
Data.m_Month = 1 + now_tm.tm_mon;
Data.m_Day = now_tm.tm_mday;
std::ofstream file("Tasks_Data.dat", std::ios::app);
if (!file.is_open())
{
std::cout << "file cannot be opened";
return false;
}
file.write((char*)(&Data), sizeof(DoList));
file.close();
return true;
}
the function where I read data
void DoList::Read_Data()
{
DoList Read;
std::ifstream file("Tasks_Data.dat");
if (!file.is_open())
{
std::cout << "file cannot be opened";
}
while (file.read((char*)(&Read), sizeof(DoList)))
std::cout << "Task ID: " << Read.m_ID << ", Task Name: " << Read.m_TaskName << ", Description: " << Read.m_Description << ", Status: " << Read.m_Status << ", Date: " << Read.m_Year << "/"
<< Read.m_Month << "/" << Read.m_Day << std::endl;
file.close();
}