Dynamic Objects!
Hi,
How do I store dynamically generated lists in to files?
This is my class ...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
#include<iostream>
#include<string>
using namespace std;
class CMp3
{
struct CElement
{
char SongName[30];
char SongInterpret[30];
double SongLength;
CElement *next;
};
CElement *list;
public:
void PushFront(const char name[30],const char interpret[30],double length);
void PrintList();
void PushBack(const char name[30],const char interpret[30],double length);
void SaveInFile();
void LoadFromFile();
void PushBack();
CMp3():list(NULL)
{
}
~CMp3();
};
|
And the objects are being created like this...
1 2 3 4 5 6 7 8 9 10 11
|
void CMp3::PushFront(const char name[],const char interpret[],double length)
{
CElement *elem = new CElement;
elem->next = list;
list = elem;
strcpy_s(list->SongName,name);
strcpy_s(list->SongInterpret,interpret);
list->SongLength = length;
}
|
Thanks a lot!
Agree that there is already code and classes for this. But in order to serialize into a stream your current code, you could:
1 2 3 4 5 6 7 8 9
|
void SerializeList(const CElement *list, ostream &os)
{
const CElement *current = list;
while (current)
{
os << current->SongName << current->SongInterpret << current->SongLength;
current = current->next;
}
}
|
That probably works OK.
Topic archived. No new replies allowed.