qt把自定义类输入到二进制文件中,从二进制文件中读取类

// mlist.h文件
#ifndef MLIST_H
#define MLIST_H
 
  
 
  
#include
#include
#include
using namespace std;
 
  
template<class T>
struct List
{
    T data;
    List<T> *next;
};
 
  
template<class T>
class MList
{
private:
    List<T> *root;
    int len;//长度
    void deleteAll()
    {
        List<T> *p=root;
        if(p==NULL){delete p;return;}
        else {
            do{
                root=root->next;
                delete p;
                p=root;
            }while(root);
        }
    }
public:
    MList(){
        root=NULL;
        this->len=0;
    }
//    MList(MList &other){}
    ~MList(){deleteAll();}
    List<T> *createNewList(const T &val){
        List<T> *p=new List<T>;
        p->data=val;
        p->next=NULL;
        return p;}
    int length()const//不改变内容
    {
        return len;
    }
    int size()const{return len;}
    void append(const T &val){
        if(this->root==NULL){this->root=createNewList(val);}
        else {
           List<T> *p=root;
           while(p->next)
           {
               p=p->next;
           }
           p->next=createNewList(val);
        }
        len++;
    }
 
  
    void prepend(const T &val)
    {
        if(this->root==NULL)
            this->root=createNewList(val);
        else
        {
           List<T> *p=createNewList(val);
           p->next=root;
           root=p;
        }
        this->len=this->len+1;
    }
 
  
    const T& at(int index)const
    {
        if((index<0)||(index+1>length()))throw "index的大小应>0小于length()";
        else
        {
            List<T> *p=this->root;
            for(int i=0;i 
  
            {
                p=p->next;
            }
            return p->data;
        }
    }
    void removeAt(int index)
    {
        if((index>len-1)||index<0)return;
        else if(index==0) {removeFirst();return;}
        else if(index==len) {removeLast();return;}
        {
            List<T> *p=root;
            for(int i=0;i1;i++)//找到该节点的前一个
            {
                p=p->next;
            }
 
  
            List<T> *p_=p->next->next;
            delete p->next;
            p->next=p_;
            len--;
        }
    }
 
  
    void remove(int from,int to)
    {
        if(from>to)return;
        if(from<0)return;
        if(to>len)return;
        for(int i=to-from+1;i>0;i--)
        {
            removeAt(from);
        }
    }
 
  
    void removeFirst()
    {
        if(len==0)return;
        List<T> *p=root;
        root=root->next;
        delete p;
        len--;
    }
 
  
    void removeLast()
    {
        if(len==0)return;
        if(len==1){delete root;root=NULL;len--;return;}
        List<T> *p=root;
        while(p->next->next)
        {
            p=p->next;
        }
        List<T> *p_=p->next;
        delete p->next;
        p_->next=NULL;
        len--;
    }
 
  
    void replace(int index,T &val)
    {
        if((index>len-1)||(index<0))return;
        else
        {
            List<T> *p=root;
            for(int i=0;i 
  
            {
                p=p->next;
            }
            p->data=val;
        }
    }
    int count(const T &val)const{
        int sum=0;
        List<T> *p=root;
        while(p)
        {
            if(p->data==val)sum++;
            p=p->next;
        }
        return sum;
    }
 
  
    /*写入到二进制文件*/
    void writeBinaryFile(QString &binaryFilename){
        QFile file(binaryFilename);
        if(!file.exists())return;
        file.open(QIODevice::WriteOnly);
        List<T> *p=root;
        while(p)
        {
            file.write((char*)(&(p->data)),sizeof(T));
            p=p->next;
        }
        file.close();
    }
    /*从二进制文件中读取*/
    void readBinaryFile(QString &binaryFilename){
        QFile file(binaryFilename);
        if(!file.exists())return;
        file.open(QIODevice::ReadOnly);
       while(1){
           T p;
            file.read((char*)&p,sizeof(T));
            if((p)==T()){file.close();return;}
            append(p);
        }
 
  
 
  
 
  
//        file.read();
 
  
        file.close();
    }
 
  
    //    ~MList();
};
 
  
#endif // MLIST_H



#ifndef STUDENT_H
#define STUDENT_H
 
  
#include
 
  
//class MList;
//class List;
#include"mlist.h"
 
  
using namespace std;
struct Student
{
    QString name;
    int age;
    Student(){name="";age=-1;}
    bool operator !=(const Student &val){if((val.age==age)&&(val.name==name))return false;else return true;}
    bool operator ==(const Student &val){if((val.age==age)&&(val.name==name))return true;else return false;}
};
typedef MList<Student> StudentList;
 
  
#endif // STUDENT_H

// 主函数
#include"mlist.h"
#include"stuent.h"
 
  
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
//    readWrite();
 
  
    StudentList list,list1;/*typedef MList<Student> StudentList;*/
    Student stu1,stu2,stu3;
    stu1.name="wang";
    stu2.name="hujingtao";
    stu3.name="xijinping";
    stu1.age=20;
    stu2.age=54;
    stu3.age=55;
    list.append(stu1);
    list.append(stu2);
    list.append(stu3);
    QString filename("studentlist.dat");
    list.writeBinaryFile(filename);
    list1.readBinaryFile(filename);
    for(int i=0;i 
  
    {
        qDebug()<name<age< 
  
    }
//    MainWindow w;
//    w.show();
//    StudentSizeList list;
//    list.
    return a.exec();
}

你可能感兴趣的:(下东西)