Solved

c++ file handling a simple library information system

Posted on 2010-11-20
13
1,330 Views
Last Modified: 2012-05-10
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.
0
Comment
Question by:Dinesh Kumar
  • 5
  • 5
  • 3
13 Comments
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
Insert into what ? Update what ? Delete what ?

Are you talking about interaction with a database ? Which database ?
0
 
LVL 9

Expert Comment

by:masheik
Comment Utility
Hi,

   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,
c++:
-----
http://www.functionx.com/cpp/articles/linkedlist.htm
http://www.cplusplus.com/doc/tutorial/files/
c:
---
http://www.cprogramming.com/tutorial/lesson15.html
http://www.codeproject.com/KB/cpp/linked_list.aspx

  Create a linked list structre which all the datas you have mentioned,
struct libraryDB
{
  int bookNum;
 char *authName;
 etc..
 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
0
 

Author Comment

by:Dinesh Kumar
Comment Utility
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.
0
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
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.
0
 

Author Comment

by:Dinesh Kumar
Comment Utility
its for academic assignment.  every book will be having its own unique id. it will be used on a single system.
0
 
LVL 9

Accepted Solution

by:
masheik earned 267 total points
Comment Utility
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" ;

    myfile.close();
  }
  else cout << "Unable to open file";
  return 0;
}
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:Dinesh Kumar
Comment Utility
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

Menus:
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.
Article583-2792010.zip
0
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
>> 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 :

        http://www.cplusplus.com/doc/tutorial/files/
0
 

Author Comment

by:Dinesh Kumar
Comment Utility
and how can we use the concept of inheritance?
0
 
LVL 9

Assisted Solution

by:masheik
masheik earned 267 total points
Comment Utility
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
0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 133 total points
Comment Utility
>> 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 ?
0
 

Author Closing Comment

by:Dinesh Kumar
Comment Utility
I did not find the complete solution.
0
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
>> 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 ?
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now