Witam
Jestem początkującym programistą dlatego proszę o wyrozumiałość.
Od kilku dni borykam się z problemem zapisania i oczytania listy dwukierunkowej do/z pliku (pisze program na zaliczenie).
Po prostu muszę gdzieś przechować dane gdy zamykam program, jeśli ma ktoś lepszy pomysł to chętnie wysłucham.
Błąd Block type is valid(pHead->nBlockUse) zaznaczyłem w miejscu w którym się pojawia.
Z góry dziękuje za pomoc.
Funkcja odpowiada za zapis listy do pliku:
file.h
#ifndef file_h
#define file_h
#include<iostream>
#include <fstream>
#include"list.h"
using namespace std;
void save(list<element_cs, customer_data> to_save);
list <element_cs, customer_data> open();
#endif
file.cpp
#include "file.h"
void save(list<element_cs, customer_data> to_save)
{
ofstream file_;
file_.open("data_company.date", ios::binary | ios::out | ios::trunc);
if (!file_.good())
{
cout << "Nie mozna stworzyc pliku!!!\n";
getchar();
}
else
{
element_cs *step = to_save.head;
while (step)
{
file_.write(reinterpret_cast <char *>(&step->someone), sizeof(customer_data));
file_.write(reinterpret_cast <char *>(&step->something), sizeof(article_data));
file_.write(reinterpret_cast <char *>(&step->i), sizeof(int));
file_.write(reinterpret_cast <char *>(&step->status), sizeof(int));
file_.write(reinterpret_cast <char *>(&step->next), sizeof(element_cs));
step = step->next;
}
cout << "Zapisano dane do pliku\n";
}
file_.close();
}
list <element_cs, customer_data> open()
{
list <element_cs, customer_data> bufor;
ifstream file_;
file_.open("data_company.date", ios::binary | ios::in);
if (!file_.good())
{
cout << "Brak pliku!!!\n";
getchar();
}
else
{
element_cs step;
while ( step.next != NULL)
{
file_.read(reinterpret_cast <char *>(&step.someone), sizeof(customer_data));
file_.read(reinterpret_cast <char *>(&step.something), sizeof(article_data));
file_.read(reinterpret_cast <char *>(&step.i), sizeof(int));
file_.read(reinterpret_cast <char *>(&step.status), sizeof(int));
file_.read(reinterpret_cast <char *>(&step.next), sizeof(element_cs));
bufor.addend(bufor.create_theelement(step));
}
} //tutaj wywala bład !!!!!!!!!!!!!!!
file_.close();
return bufor;
}
Błąd powstaje gdzie przy tworzeniu listy wiec dołacze jeszcze kod klasy szablonowej listy dwukierunkowej;
#ifndef list_h
#define list_h
#include<iostream>
#include<string>
#include<vector>
#include "customer_data.h"
#include "company_data.h"
#include "article_data.h"
#include "status_complaint.h"
struct element_cs
{
element_cs * next, *prev;
customer_data someone;
vector <article_data> something;
int i=0;//indeks do vectora
int status = registered;
};
struct element_cm
{
element_cm * next, *prev;
company_data someone;
vector <article_data> something;
int i = 0;//indeks do vectora
int status = registered;
};
template< class Typ1, class Typ2 >
class list
{
public:
Typ1 *head, *tail;
list();
~list();
Typ1* create_element(Typ2 someone_l, article_data something_l);
Typ1* create_theelement(Typ1 cr);
void addend(Typ1 *s_element);
void view_elemnt(Typ1 *s_element);
};
template< class Typ1, class Typ2 >
list<Typ1, Typ2>::list()
{
head = NULL;
tail = NULL;
}
template< class Typ1, class Typ2 >
list<Typ1, Typ2>::~list()
{
Typ1 *p;
while (head)
{
p = head->next;
delete head;
head = p;
}
}
template< class Typ1, class Typ2 >
void list<Typ1, Typ2>::addend(Typ1 *s_element)
{
if ((this->head) == NULL && (this->tail) == NULL)
{
this->head = s_element;
this->tail = s_element;
}
else
{
this->tail->next = s_element;
s_element->prev = this->tail;
s_element->next = NULL;
this->tail = s_element;
}
}
template< class Typ1, class Typ2 >
Typ1* list<Typ1, Typ2>::create_element(Typ2 someone_l, article_data something_l)
{
Typ1 *a = new Typ1;
a->next = NULL;
a->prev = NULL;
a->someone = someone_l;
a->something=something_l;
a->i++;
return a;
}
template< class Typ1, class Typ2 >
Typ1* list<Typ1, Typ2>::create_theelement(Typ1 cr)
{
Typ1 *a = new Typ1;
a->next = NULL;
a->prev = NULL;
a->someone = cr.someone;
a->something = cr.something;
a->i = cr.i;
a->status = cr.status;
return a;
}
template< class Typ1, class Typ2 >
void list<Typ1, Typ2>::view_elemnt(Typ1 *s_element)
{
int j;
cout << "\nDane reklamacji: \n";
cout << "Status reklamacji: ";
view_status(s_element->status);
s_element->someone.view();
for (j = 0; j < s_element->i; j++)
{
s_element->something[j].view_art();
}
}
#endif