Frage

I want to put my double linked list of Students into file and then read them. My double linked list works fine, has every crucial feature now. Relevant to this task may be:

void addToChain(Student& BeeingAddedStudent);
void removeLastLink();
int  howManyLinks();
friend ostream& operator << (ostream&, Chain&);
friend ofstream& operator << (ofstream&, Chain&);

Student looks like this:

class Student
{
    string First;
    string Last;
    string* Classes; //array of classes student attend **(MAY BE PHRASES WITH FEW WORDS)**
    int Id, N; //student's ID in university system and number of classes
    double Avg; //average grade

public:
    Student();
    Student(Student&);
    ~Student();

    ... //Full list of accessors

    Student& operator=(Student&);
    friend ostream& operator << (ostream&, Student&);
    friend ofstream& operator << (ofstream&, Student&);

}

Now, what do you think will be best choice to store this objects? I don't want to have an insight into a file. Can you please explain me, how exchange this objects with files? I know basic commands, but have no idea which methods of fstream should i use. Here's, what i started with, so your answer can be short and comprehensive:

void Chain::saveToFile()
{
    ofstream file;
    file.open("Data.cha");
    file << *this;
    file.close();
}

void Chain::loadFromFile()
{
    while (StartLink->Essence != NULL)
        this->removeLastLink();
    ifstream file;
    file.open("Data.cha");
    Student current;
    while (!EOF)
    {
        //I have no idea, what should i put here...
    }

}

As you see, i started working with textfiles, but if you think, that's fine, i will love to change it to binary...

War es hilfreich?

Lösung

"Best choice" depends on what you rate high.

You can add to/fromBinaryFile methods to your class, for which you'll have to define a byte array memory layout. This should be fastest.

You can add to/fromText methods to your class, for which you'll have to define textual representations. This will be tolerant against changes in the internal representation, but conversion to and from text requires extra effort. It'll also (usually) require a little more disk space.

You can define an XML layout and use some XML library for reading and writing XML documents. This is the most complex, but you'll be free from all worries about textual representation, and data will be ready for interoparibility with any other program capable of reading/writing XML and there are nice things like XSLT. In a sense, this will also be tolerant against extensions of your class.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top