Thread: The best way to delete a record from a file?

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    84

    The best way to delete a record from a file?

    Hi ALL,

    Lets say I have a file name "cars.bin".
    Each record in the file is a struct (made from int, char [20], int).
    Lets say I have 10000 records,
    struct 1, struct 2, struct 3, struct ....., struct 10000.

    Now, If I want to delete record #400, how should I do it??
    The only thing I could think of is to:
    1. Create a new file
    2. Copy all the records from old file to new file (except of the #400)
    3. Delete old file
    4. Rename new file to old file name.

    This seem like a slow poor solution, is there any other way to do it?

    Many thanks
    Salvador

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Since all the structures are the same size, you could open the file, seek to structure 10,000, copy it to structure #400 position, then resize the file to be smaller by one structure.

    This would be very fast.

    Todd

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You could have a marker that says "deleted" in each record [e.g. the first int being a negative number, just as an example]. Then when you are reading the records into your application, check the "deleted" marker and skip those that are deleted.

    If you want to, you could have a "compress" mechanism, so that when you exit the applicaiton, if you counted more than X% deleted records, THEN you rewrite the whole file without the deleted records.

    Or, if you want to re-use deleted records, you could keep a linked list of records that have been deleted, e.g. using the second int to indicate which record is the next free one, so when you add new records, you overwrite the first record in that list.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Nov 2006
    Location
    japan
    Posts
    126
    hummm... I probably would copy struct401 to struct400 then struct402 to struct140 and so on...
    I have no much experience with handling files but if the structs were linked with pointers is could be easier and faster. Unfurtunately in the case of files i don't think this is like this.
    Mac OS 10.6 Snow Leopard : Darwin

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Todd Burch View Post
    Since all the structures are the same size, you could open the file, seek to structure 10,000, copy it to structure #400 position, then resize the file to be smaller by one structure.

    This would be very fast.

    Todd
    Although resizing files is only supported by some OS's.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    This dilemma is why databases were invented.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, and most databases don't ACTUALLY delete things, they just mark it as "deleted", and re-use as needed.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    84
    Cheers!

    How do I resize a file?

  9. #9
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Since all the structures are the same size, you could open the file, seek to structure 10,000, copy it to structure #400 position, then resize the file to be smaller by one structure.

    This would be very fast.

    Todd
    I don't get exactly how this would work. Would you not need to move the structure instead of copy it?

    Say each entry is a line of text, then to actually delete it you would have to copy all the following text over it, then resize the file. Or am I missing something here?

    Personally I like matsp's suggestion, which sounds easy to do.

  10. #10
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    On unix, you would use truncate().

  11. #11
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Quote Originally Posted by mike_g View Post
    I don't get exactly how this would work. Would you not need to move the structure instead of copy it?

    Say each entry is a line of text, then to actually delete it you would have to copy all the following text over it, then resize the file. Or am I missing something here?

    Personally I like matsp's suggestion, which sounds easy to do.
    move==copy. Same thing.

  12. #12
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Oh yeah, actually I get it now (i think). You could just swap the entry to be deleted with the last entry then truncate it. So yeah both ways would be fast

  13. #13
    Registered User
    Join Date
    Dec 2007
    Posts
    84
    I will adapt mat's suggestion too if the file is sorted. (thank you)
    and Todd's if not (thank you too)
    I still don't understand how can I resize a file size on Windows....

  14. #14
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    On Windows, _chsize().

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by mike_g View Post
    Oh yeah, actually I get it now (i think). You could just swap the entry to be deleted with the last entry then truncate it. So yeah both ways would be fast
    But then the records would be out-of-order. Record 1 ... 400, 900, 401 ... 899.
    The best and easiest way is to just use a flag to indicate if the record is valid or deleted. You can then write new data there later or just move data down if the records needs to be in order.
    Again, if they don't need to be in order, mike_g's suggestion works, as well.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Update Record & Delete Record in File.
    By unsafe_pilot1 in forum C Programming
    Replies: 13
    Last Post: 05-18-2008, 07:22 AM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  4. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  5. how to delete or modify a record in a file?
    By danielhf in forum C++ Programming
    Replies: 2
    Last Post: 09-22-2003, 09:18 AM