c++ file handling a simple library information system

can you provide me a simple program in c++ providing insert,update,delete and show the books?
the records should be kept in a file on the disk.
Dinesh KumarAsked:
Who is Participating?
Ok..Read the File handling operations in C++,

How to read,write,append, remove,etc,

create a structure about books information and store data into it

/* Example Store data to file */

#include <iostream>
#include <fstream>
using namespace std;

struct bookInfo

 char title[255];
 unsigned int  price;
 int accessNum;

int main ()
  ofstream myfile ("example.txt",ios::out);
  bookInfo bInfo;
  cout <<"Enter title: ";
  cin  >>  bInfo.title ;
  cout <<"Enter price: ";
  cin  >> bInfo.price;
  cout <<"Enter accessNum:";
  cin  >> bInfo.accessNum;
  if (myfile.is_open())
    myfile << bInfo.title ;
    myfile << "\t" ;
    myfile << bInfo.price;
      myfile << "\t" ;
    myfile << bInfo.accessNum;
      myfile << "\n" ;

  else cout << "Unable to open file";
  return 0;
Insert into what ? Update what ? Delete what ?

Are you talking about interaction with a database ? Which database ?

   If i understand your question correctly, you can use linked lists to create a database dynamically and store database of what you have entered and retrieve the same,
  You need to go through what are linked lists , refer a good book about linked lists
 the following links can help to learn the basics,

  Create a linked list structre which all the datas you have mentioned,
struct libraryDB
  int bookNum;
 char *authName;
 struct libraryDB *nextlibraryDB; //link to the structure

add function to add,delete ,insert,sort the linked lists

 try that all mentioned and comeback if you have issue ,we experts are here to help you
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Dinesh KumarAuthor Commented:
I want to keeps books' information like title, price, accession number in a file. so i need to use the file handling in c++. suppose I need to update the title of the book then i will open the file through c++ code in write only mode and will go to that specific records by using some id say accession number and update that particular book information.
Is this for professional purposes, a personal project, an academic assignment ?

Depending on what this is for, the answer will be very different. So a bit more background information would be useful.

How many books do you want to store ? And how do you want to access that information ? Is the access local only, or do you need access from different locations ? What kind of access do you need precisely ? Etc.
Dinesh KumarAuthor Commented:
its for academic assignment.  every book will be having its own unique id. it will be used on a single system.
Dinesh KumarAuthor Commented:
Masheik, I am working on following. Actually i just  noticed that I need to use inheritance concept.

How can i use inheritance in following problem?

Make a menu-driven project to create a library information system containing the following information for the proper storage and processing of books in the library.
(a) Accession Number                  (b) Name of the Author
(c) Title of the Book                  (d) Publisher Name
(e) Publisher Name                  (f) Cost of the Book
(g) Year of Publication            (h) Number of pages
Use the of inheritance

1) Add a new Book
2) Display all the books
3) Delete a Book Details (Soft Delete only)
4) Search a book using Accession Number

the code i attached is to be still modified.
>> its for academic assignment.  every book will be having its own unique id. it will be used on a single system.

I would start by implementing the "Add a new Book" feature. You'd need a Book class that represents a book. Its data members should describe all specifics of the book (title, author, year, etc.). And it should have some methods for manipulating the book (nothing special probably - just provide ways of retrieving and modifying the data members).

You can then write some code that asks the user to enter the details for a new book. While the details are being entered, you set them in a new instance of the Book class (via its constructor possibly). Once the Book instance has been filled, you can write some code that writes the information for the book to a file (or : that serializes a Book instance to a file).

When you have that, you can write code that retrieves a book from the file, creates a new Book instance for it, and keeps it in memory, where it can be manipulated.

The next step would be to have an in-memory data structure that can hold many Book instances, and provides a convenient way for performing lookup. Which data structure you choose will probably depend on what data structures you have already worked with during your classes.

You can then adapt your code to read multiple books from a file, and store them in the data structure. And to be able to write all books from the data structure back to a file.

All that is left after this is done, is some small features, which you can then add one by one.

Since your original question was about file handling in C++, I recommend that you have a look at this tutorial to guide you :

Dinesh KumarAuthor Commented:
and how can we use the concept of inheritance?
Okay inheritance is the process of creating new classes from a base class .go thru inheritance concepts as well.                           In your case you need to add cool entry remove book entry display all the books and search for a book
Say you have written only add and remove book methods in your base class and it is well tested /working fine
But you need to have search and display methods  
Create a derived class with using the base class you have written and add display ,search methods to the derived class this way you can use inheritance concepts
>> and how can we use the concept of inheritance?

I can't think of a reasonable way of using inheritance for this - except maybe if you want to deal with other things besides books too (cd's, dvd's, etc. maybe).

How does the teacher say you should be using inheritance ?
Dinesh KumarAuthor Commented:
I did not find the complete solution.
>> I did not find the complete solution.

Then why didn't you ask us for clarification about the parts that haven't been addressed ? Why didn't you respond to our last posts ?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.