2012-11-01 17 views
3

問題:我正在嘗試寫入(二進制寫入)雙重鏈接列表對象到文件&。 我必須寫入對象的完整內容,然後從文件中加載它,並將其存儲到新對象中,以便按照FIFO順序重新創建列表。 我以爲自己寫的是正確的,但我真的不知道如何從文件中加載(讀取)它。在C++中使用LinkList進行文件處理

記住:我只是想保存和讀取節點的CONTENTS,& NOT POINTERS.

CODE:

//template type BOOK class 

template<class mytype> 
class BOOK  
{ 
private: 
    static int count; //declaration of static variable to set ID for books 
public: 
    BOOK<mytype> *next, *prev; //BOOK type pointers; 'next' to store address of 
next BOOK & 'prev' to store address of previous BOOK 
    int ID;   //variable to store ID of a book 
    string bookName;//string to store name of a book 
    string author; //string to store name of author of book 
    string book_type;//string to store type of a book 
    long copies; //variable to store no. of copies a book 
    long price;  //variable to store price of a book 
    string status; //to store status of a book, either its in stock or not 
    dynamicQueue<string> book_queue; //created an object of queueClass as data member of each Book 

    BOOK() //Constructor 0 argument to initialize everything 
    { 
     count++; //increment counter 
     ID=count; //assign counter to ID to be ID of newly added book 

     next = prev = 0;  //Initializing both pointers to 0 

     bookName = "\0"; 
     author = "\0"; 
     book_type = "\0"; 
     copies = price = 0; 
     status= "InStock"; 
    } 

    BOOK(BOOK *n = 0, BOOK *p = 0, string book = "\0", string athr = "\0", string buk_type = "\0", long cp=0, long pr=0) //Constructor multiple arguments, to store information about a book 
    { 
     next = n;  //store contents of user-given value n into next 
     prev = p;  //store contents of user-given value p into previous 

     bookName = book;//store contents of user-given value book into bookName 
     author = athr; //store contents of user-given value athr into author 
     book_type = buk_type;//store contents of user-given value buk_type into book_type 
     copies = cp; //store contents of user-given value cp into copies 
     price = pr;  //store contents of user-given value pr into price 
     status= "InStock"; 
     count++;  //increment counter 
     ID=count;  //assign counter to ID to be ID of newly added book 
    } 
}; 

template <class mytype> // declaration of 
int BOOK<mytype>::count=0; // static variable to set ID for books 
//-------------------- 

主要部分用於添加新圖書。

BookStoreDataBase<char> obj; //created object of Doubly linked list 
string Book, Author, Booktype; 
long Copies=1, Price=0; 
cout<<"Enter Name of Book = "; cin>>Book; 
cout<<"Enter Author = ";  cin>>Author; 
cout<<"Enter Type of Book = "; cin>>Booktype; 
cout<<"Enter Number of Copies = "; cin>>Copies; 
cout<<"Enter Price (PKR) = "; cin>>Price; 

obj.addBook(Book, Author, Booktype, Copies, Price); 

保存功能保存所有數據保存到文件

template <class mytype> 
void DoublyLinkedList<mytype>::save_data() 
{ 
    NODE<mytype> * temp = head; //made copy of head 
    fstream file;  //created new file 
    file.open("mydata.txt", ios::binary | ios::out | ios::app); 

    while(temp->next!=0) //Until link list end 
    { 
      file.write(reinterpret_cast<char*>(&temp), sizeof(temp)); 
      temp = temp - > next; //move temp to next node 
    } 
    file.write(reinterpret_cast<char*>(&temp), sizeof(temp)); //write again for last 
                   //book's data 
    file.close(); 
} 

現在,我對如何與干擾保存設置讀取文件列表,存儲內容到每個節點&幾乎沒有想法,按照FIFO順序重新創建列表。所以我可以稍後打印它。我練習了很多,去過論壇等,但沒有找到具體的解決方案。請幫助我。在此先感謝


我的努力

template <class mytype> 
void DoublyLinkedList<mytype>::load_data() 
{ 
    fstream file; 
    file.open("mydata.txt", ios::binary | ios::in); 
    while(!file.eof()) 
    { 
     NODE<mytype> *temp = new NODE<mytype>; 
     file.read(reinterpret_cast<char*>(&temp), sizeof(temp)); 
     if(is_Empty()) 
     { 
      head = tail = temp; 
     } 
     else 
     { 
      temp->prev->next = temp; 
      temp->next=0; 
     } 
    } 
    file.close(); 
} 
//------------------- 

NO編譯錯誤的樣本。

運行時錯誤:在DobulyList.exe 0x00CD8391未處理的異常:0xC0000005:訪問衝突寫入位置0x00000004。

+0

我不認爲它是可能的二進制編寫一個類與std ::字符串中,並有字符串數據包括在輸出中。爲什麼預訂模板課程? – Scooter

+0

Book類的行爲類似於與原始DoublyClass(DataBase類)一起使用的NODE類或結構來管理單個節點/書籍。 – InamTaj

回答

0

我認爲你最好的選擇是在BOOK類上有一個方法,它可以以預定義的方式將內容序列化。

template<class mytype> 
class BOOK  
{ 
private: 
    //members 
public: 
    //more members 

    .... 

    bool read(istream& in); 
    bool write(ostream& out); 

    .... 

}; 

所以,因爲你想二進制 - 你需要有和INT的ID,然後大小寫的,爲的字符串(S)等字節......

然後你扭轉這一對讀。讀一個int,並分配給ID,然後讀取大小,讀取大小字符和分配給字符串等...

//fill in template stuff I'm lazy... 
bool Book::read(istream& in) 
{ 
    size_t stringSize; //you need to think about what types you read/write 
    char buffer[SomeArbitrarySize]; 
    file.read(ID ,sizeof(int)); 
    file.read(stringSize ,sizeof(size_t)); 
    file.read(buffer, stringSize); 
    mStringMember = buffer; 
    ... etc ... 
} 

然後在列表中每個項目委派調用讀/寫入節點。您可能還需要首先爲節點數量寫入/讀取條目。

template <class mytype> 
void DoublyLinkedList<mytype>::load_data() 
{ 
    fstream file; 
    file.open("mydata.txt", ios::binary | ios::in); 
    while(!file.eof()) 
    { 
     NODE<mytype> *temp = new NODE<mytype>; 
     temp->read(file); 
     if(is_Empty()) 
     { 
      head = tail = temp; 
     } 
     else 
     { 
      temp->prev->next = temp; 
      temp->next=0; 
     } 
    } 
    file.close(); 
} 
+0

Bro我完全不明白剛剛說的:D count是一個靜態變量,每次在_BookStoreDatabase Class_中創建一本書時,它都會增加。當它增加時,我把它的一個副本放在書的節點中作爲它的ID。所以書的ID是自動生成的。示例: 書ID 1 - > OOP在C++ 書ID 2 - 在C>數據結構++ 我也寫在_BookStoreDataBasse Class_一個方法,通過它的編號來搜索一本書,或者以自己的名義,或它的作者。 – InamTaj

+0

好的,當我發佈時我很匆忙 - 我的意思是,Book類的每個元素可能需要按預定順序單獨寫入文件,然後以相同的順序讀回。我會讓答案更具可讀性...... – Caribou