使用C 中的链接列表的文件处理

File Handling with LinkList in C++

本文关键字:文件 处理 列表 链接 使用      更新时间:2023-10-16

问题:我试图写(二进制写作)双链接列表的对象到文件&也从中写下。我必须编写对象的完整内容,然后将其从文件加载,然后将其存储到新对象中以按FIFO顺序重新创建列表。我以为我写作正确,但我真的不知道如何从文件加载(阅读)。

记住:我只是想保存和阅读节点的CONTENTS,&NOT POINTERS.

代码:

//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 = "";
        author = "";
        book_type = "";
        copies = price = 0;
        status= "InStock";
    }
    BOOK(BOOK *n =  0, BOOK *p = 0, string book = "", string athr = "", string buk_type = "", 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();
}

现在,我几乎不知道如何从文件中读取列表,将内容存储到每个节点&amp;在干扰保存的安排的情况下,按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();
}
//-------------------

无编译时间错误。

运行时错误: in Dobulylist.exe中的0x00CD8391未经处理的异常:0xc00000055:访问违规写作位置0x00000004。

我认为您最好的选择是在书类上使用一种可以以预定义的方式序列内容的方法。

template<class mytype>
class BOOK      
{
private:
    //members
public:
    //more members
    ....
    bool read( istream& in );
    bool write( ostream& out );
    ....
};

因此,因为您想要二进制 - 您需要为ID编写并写入int,然后为字符串(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();
}