0

本の属性がノードなどに保存されるリンクリストベースの BookStore を設計しました。さらに、プログラムの最後に、すべてのデータベースをテキストファイルに保存する必要があります(バイナリの読み取りを試みましたが、殺されて実行できませんでした)、各本のすべての情報を1つずつリロードして保存しますノード & LinkList を再作成します。これで保存が完了し、まったく問題ありません。しかし、テキストファイルからの読み取りに問題があります。

構造をファイルに保存する:::

BookID(int) - BookName(string) - Author(string) - BookType(string) - Copies(long) - Price(long) - '\n' (次の行に移動)

例: 1 ObjectOrientedParadigm R.Lafore Coding 5 900 2 ObjectOrientedParadigm R.Lafore Coding 5 900 など......

これが保存機能です。

bool BookStoreDataBase<mytype>::save_all_data()
{
    if(!is_Empty()) //if list is not empty
    {
        BOOK<mytype> *temp = head;   //created a copy of head
        ofstream file("database.txt", ios_base::app); //created file, to write at the end (append)
        while(temp != tail) //while list ends
        {
            file<<temp->ID<<' '<<temp->bookName<<' '<<temp->author<<' '<<temp->book_type<<' '<<temp->copies<<' '<<temp->price<<' ';  //write all info
            temp = temp->next; //move temp to next node
        }
        file<<temp->ID<<' '<<temp->bookName<<' '<<temp->author<<' '<<temp->book_type<<' '<<temp->copies<<' '<<temp->price<<' '; //for last book's info
        return true; //to confirm sucessfull writing
    }
    else //if list is empty
    {
        return false; //to confirm error in writing
    }
}

問題:: 読み取りを開始すると、最初の行は正常に読み取られ、リストに保存されますが、次回は次の行から読み取るファイルを作成できません。したがって、'\n' です。&それが問題を引き起こします。ファイルは最初の行を再度読み取り、2 番目のノードは同じデータで作成されます。

ローディング機能:

void BookStoreDataBase<mytype>::load_all_data()
{
    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


    ifstream file("database.txt");
    while(file) //I have tried file.eof but its not working, don't know why
    {
        file>>ID>>bookName>>author>>book_type>>copies>>price>>status; //read file

        BOOK<mytype> *temp = new BOOK<mytype>(0, 0, bookName, author, book_type, copies, price);  //create a new node in memory and save all the data

        if(is_Empty()) //if list is empty, then make 1st node
        {
            head = tail = temp;
        }
        else //other wise make the next node
        {
            tail->next = temp;
            temp->prev = tail;
            tail = temp;
        }
    }
}

MOREOVER 読み取りは、実際のレコードの 1 倍少なく行われます。つまり、.txt に 4 つのブックのレコードがある場合、3 つのノードが作成されます (& すべてのノードで最初の情報のみが繰り返されます) が、4 つのノードを読み取って作成する必要があります!

私は初心者です。どんな助けでも大歓迎です。

4

2 に答える 2

2

std::getline()を使用して行全体を取得し、次にstringstreamclass を使用してそのすべてをそれぞれの変数に読み込むことをお勧めします。

于 2012-11-03T12:35:51.107 に答える
1

while (!file.eof())違う、while (file)違う。すべての新しいプログラマーは、ファイルから読み取る正しい方法を理解できていないようです。理由がわかればいいのですが、初心者にアドバイスするのは簡単です。基本的な誤解は、初心者が最初にファイルの終わりをテストしてから2 番目に読むべきだと考えているようです。実際には、最初に読み取り、次に読み取りが失敗したかどうかを確認する必要があります。

これはファイルから読み取る正しい方法です

while (file >> ID >> bookName >> author >> book_type >> copies >> price >> status)
{
}

それを試して、まだ残っている問題を確認してください。

別の問題に気づきました。文字列であると言うステータスを読み取ろうとしますが、ファイル形式の説明にはステータスがありません。それがあなたの本当の問題だと思います。

于 2012-11-03T12:41:29.533 に答える