Deleting Entries from a database

This is a discussion on Deleting Entries from a database within the C++ Programming forums, part of the General Programming Boards category; hey guys i have created (or half made) an address book program. It saves the name, address etc into a ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    13

    Deleting Entries from a database

    hey guys
    i have created (or half made) an address book program.
    It saves the name, address etc into a text file.

    http://www.x-kane.com/programming/addressbookmain2.cpp

    but the parts i am stuck on now, is how to edit and delete records.

    how would i go about deleting one of the entries? would i have to copy the contents of the old file to a new file, minus the parts i dont want in it?
    or is there a better way to just delete say the 5/6 lines and move the next entry up 5/6 lines?

    thanks alot
    Kane

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,755
    > would i have to copy the contents of the old file to a new file, minus the parts i dont want in it?
    Yes, that's pretty much the way to do it (in a simple way).

    Another way is to store a "deleted" flag along with each record, which doesn't involve any rewriting of the file (you just set the deleted flag). The old data remains, but is hidden from view because your application just skips records which are marked deleted.
    It is however much faster than rewriting the whole file for each change.

    However, your files continue to grow as more and more records are deleted, so most such applications have some kind of "compact database" option which basically just rewrites the entire file without the deleted records.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    13
    ah ok thanks,

    so if i set a delete flag, i could also put another flag in which say might keep track of how many delete flags there are.

    and so to stop the file growing overly large, when this flag reaches a certain number, it could then 'compact' the databse.

    thanks alot
    Kane

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,755
    Well you would know how many deleted records there were, along with the number of live records simply by reading the file.

    Then you can print "compaction will save xxx KBytes" quite easily.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    if you want to get really nifty, you can overwrite the old records with new ones, or alternatively, have some way of removing the 'deleted' flag so users can 'undelete' something if they realize they need it before their next compaction...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. literature database: help with planning
    By officedog in forum C++ Programming
    Replies: 1
    Last Post: 01-23-2009, 12:34 PM
  2. Creating a database
    By Shamino in forum Game Programming
    Replies: 19
    Last Post: 06-10-2007, 02:09 PM
  3. Missing Entries in Hashtable
    By wuzzo87 in forum C Programming
    Replies: 3
    Last Post: 05-13-2007, 01:46 AM
  4. Replies: 10
    Last Post: 05-19-2006, 12:23 AM
  5. Developing database management software
    By jdm in forum C++ Programming
    Replies: 4
    Last Post: 06-15-2004, 05:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21