2017-09-14 69 views
0

我做了一個經典的鏈表來模擬一個可以得到任何你想要的類型的數組。當然,你可以刪除和添加器官,所有方法都可以正常工作。保存並從鏈表中加載數據到一個二進制文件C++

現在我必須編寫寫入和讀取二進制文件的方法,我真的不知道該怎麼辦 我試過各種方法寫入文件,到目前爲止我還沒有能力取得任何進展,我甚至不知道什麼是錯的代碼,

這就是我的鏈表代碼

template <class T> 
class LinkedList 
{ 
private: 
    struct node 
    { 
     T data; 
     node * next; 
    }; 
    node* head; 

public: 
    LinkedList(); 
    ~LinkedList(); 

    void add(T value); 
    int find(T value); 
    bool remove(T value); 
    void clear(); 
    bool ifEmpty() const; 
    int size() const; 
    void display() const; 
    T getData(int index) const; 

    T &operator[](int index); 


    void save(ofstream& out); 
}; 

一部分,這就是保存方法dosent工作

template <class T> 
void LinkedList<T>::save(ofstream& out) 
{ 
    node* temp = head; 
    T temp2 = temp->data; 

    for (int i = 0; i < size(); i++) 
    { 
     out.write((const char*)(temp2), sizeof(temp2)); 
     temp = temp->next; 
     temp2 = temp->data; 
    } 
} 
012的代碼

如果你們中的一些專業人員可以給我一個方向,如何做到這一點...

+3

查找「序列化」。如果'T'包含指針,則返回的指針將不會有用。 – nwp

+1

是的,我明白你的意思,但我可以拋出一個異常,不要使用指針或東西的權利? – user8093201

+0

判斷'T'是否包含指針並不容易。如果它是一個「私人」成員呢?通常你會爲各種'T'編寫'std :: string to_string(const T&t)'和'T from_string(const std :: string&s)'函數。對於像'int'這樣的一些類型,你可以自己實現,對於用戶需要的用戶自定義類型。另外看看像[麥片](https://uscilab.github.io/cereal/)這樣的圖書館。 – nwp

回答

0

你需要先寫清單的大小。這將允許你閱讀清單。如果不閱讀文件大小,您無法決定何時停止閱讀文件。

另外,您有錯誤。該生產線

 out.write((const char*)(temp2), sizeof(temp2)); 

必須

 out.write((const char*)(&temp2), sizeof(temp2)); 
           ^^ Missing 

template <class T> 
void LinkedList<T>::save(ofstream& out) 
{ 
    size_t sz = size(); 
    out.write((const char*)(&sz), sizeof(sz)); 

    node* temp = head; 
    T temp2 = temp->data; 

    for (int i = 0; i < size(); i++) 
    { 
     out.write((const char*)(&temp2), sizeof(temp2)); 
     temp = temp->next; 
     temp2 = temp->data; 
    } 
} 

潛力讀取執行。

template <class T> 
void LinkedList<T>:read(ifstream& in) 
{ 
    size_t sz = 0; 
    in.read((char*)(&sz), sizeof(sz)); 

    // Make sure that list is empty 

    for (size_t i = 0; i < sz; ++i) 
    { 
     T temp; 
     in.read((char*)(&temp), sizeof(temp)); 

     // Add temp to the list. 
     add(temp); 
    } 
} 

save一個更新的版本,應該可以解決空指針訪問的問題,如果size()返回正確的值。

template <class T> 
void LinkedList<T>::save(ofstream& out) 
{ 
    size_t sz = size(); 
    out.write((const char*)(&sz), sizeof(sz)); 

    node* temp = head; 
    for (int i = 0; i < size(); i++) 
    { 
     // Pull the data out of the pointer here. 
     // At this point, temp should not be NULL. 
     T temp2 = temp->data; 
     out.write((const char*)(&temp2), sizeof(temp2)); 
     temp = temp->next; 
    } 
} 
+0

感謝您的幫助,我試過你的方式,但它繼續破碎,我不知道爲什麼,多數民衆贊成我主要使用它,你認爲也許我寫錯了? outstream outFile(「test.txt」,ios :: binary); \t \t iList.save(outFile); \t \t outFile.close();' – user8093201

+0

@ user8093201,請嘗試調試器。你使用'ofstream'和'save()'函數對我來說看起來很好。問題很可能在其他地方。 –

+0

調試器說temp是空的ptr所以我拋出一個異常,它現在不壓碎,但問題是,當我打開創建它在gibrish中的文件,即使我嘗試寫一個txt文件只是爲了檢查,任何知道的方式肯定如果它複製正確的數據? – user8093201