Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8777915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:23:44+00:00 2026-06-13T19:23:44+00:00

I have designed a linklist based BookStore in which book’s attributes will be stored

  • 0

I have designed a linklist based BookStore in which book’s attributes will be stored in a node & so on. Plus at the end of program, I have to save all database into text file (I tried binary reading but damn I got me killed & couldn’t do it) & then reload all info, of each book one by one & store it in nodes & re-make the LinkList.
Now saving is done & no issue at all. But Im having issues in reading from text file.

Saving structure in file is:::

BookID(int) – BookName(string) – Author(string) – BookType(string) – Copies(long) – Price(long) – ‘\n’ (to go to next line)

EXAMPLE:
1 ObjectOrientedParadigm R.Lafore Coding 5 900
2 ObjectOrientedParadigm R.Lafore Coding 5 900
and so on……

Here is the saving function.

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
    }
}

PROBLEM:: When I start reading, first line is read fine & stored in list, but for the next time, I can’t make file to read from the next line, hence ‘\n’. & that creates the problem. File again reads the first line & 2nd node is created with same data.

Loading Function:

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
Reading is done 1 time less than the real record. i.e. If .txt has record of 4 books, then 3 nodes are created, (& info of only 1st is repeated in every node), whereas it should read & create 4 nodes!

I’m a beginner, any good help will be much appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T19:23:45+00:00Added an answer on June 13, 2026 at 7:23 pm

    I would suggest you get the whole line using std::getline(), and then use stringstream class to read in everything from that into the respective variables.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have designed this gui in netBeans which has a canvas , a couple
I have designed a database whose MDF file will be copied to remote offices,
I have designed an activity in which there is a listView. I want that
I have designed an online editor with which a user can create files and
I have designed a windows service in C#, which runs continiously and spawns three
I have designed a standalone application using swings which can read data from HID
We have designed an API which exposes a series of interfaces written in C#.
I have designed a data model which has almost 24 tables. I have finished
I have designed a website which has many JavaScript blocks: <script type='text/javascript'></script> Users can
I have designed a one-page homepage with lots of content, in which a contact

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.