Thread: Deleting Record in Binary File without Temporary File

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    If you just want a flat binary file, and you don't want to use a secondary file, and you don't want to read the entire file into memory, and you want to use standard C++, and you apparently do want all this with the exception of clear screen, you need to set aside one identifier that says "this record has been deleted". Your actual file would still be 88 bytes, or whatever, but you will have records that are empty. Say you use negative one after deleting record four you'd have:

    1 A
    2 B
    3 C
    -1 Empty Record

    [Edit]
    Also, your approach to this is very wrong if you are doing what I think you are doing.

    The data you are managing shouldn't be part of the manager class.
    You shouldn't be changing `this` with respect to `this` identity when you remove a record from a file being managed.
    I could go on, but I'd like to see you fix the code first.
    [/Edit]
    Last edited by phantomotap; 09-20-2019 at 05:18 PM.
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  2. #2
    Registered User
    Join Date
    Aug 2019
    Location
    inside a singularity
    Posts
    308
    If you just want a flat binary file, and you don't want to use a secondary file, and you don't want to read the entire file into memory, and you want to use standard C++, and you apparently do want all this with the exception of clear screen, you need to set aside one identifier that says "this record has been deleted".
    My homework problem:

    Write a menu driven program to implement Adding and Deletion of Records in a binary file. (Note: Deletion to be performed without a temporary file). Use the following class:

    Code:
    class Manager
    {
           int   ID;
          char Name[20];
    
    };
    Also, your approach to this is very wrong if you are doing what I think you are doing.

    The data you are managing shouldn't be part of the manager class.
    You shouldn't be changing `this` with respect to `this` identity when you remove a record from a file being managed.
    I could go on, but I'd like to see you fix the code first.
    As a solution, should I be reading the details into another instance of Manager instead of "this" and accordingly be rewriting?
    I didn't understand the part where you say "The data you are managing shouldn't be part of the manager class.
    You shouldn't be changing `this` with respect to `this` identity when you remove a record from a file being managed.". Could you please explain more? At school, reading a binary file record by record is the only thing I've learnt till now. Please redirect me to sites where I can learn more ways to do so...

    Thanks for your time @phantomotap!

  3. #3
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Quote Originally Posted by Zeus_ View Post
    My homework problem:

    Write a menu driven program to implement Adding and Deletion of Records in a binary file. (Note: Deletion to be performed without a temporary file). Use the following class:

    Code:
    class Manager
    {
           int   ID;
          char Name[20];
    
    };


    As a solution, should I be reading the details into another instance of Manager instead of "this" and accordingly be rewriting?
    I didn't understand the part where you say "The data you are managing shouldn't be part of the manager class.
    You shouldn't be changing `this` with respect to `this` identity when you remove a record from a file being managed.". Could you please explain more? At school, reading a binary file record by record is the only thing I've learnt till now. Please redirect me to sites where I can learn more ways to do so...

    Thanks for your time @phantomotap!
    Yes. You should probably be reading into a temporary instance of the class.

    There is no reason for the manager class to have the data members it has, but if that's what you were given, I suppose that is what you should use.

    The recommendation comes from object oriented design.

    If you have a `Manager s(1, "A");`, the identity of the class, the data members `(1, "A")`, should not change to a possibly random identity, say `(4, "D")`, just because the user of class deleted an unrelated record. It makes no sense and is unexpected.
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  4. #4
    Registered User
    Join Date
    Aug 2019
    Location
    inside a singularity
    Posts
    308
    If you have a `Manager s(1, "A");`, the identity of the class, the data members `(1, "A")`, should not change to a possibly random identity, say `(4, "D")`, just because the user of class deleted an unrelated record. It makes no sense and is unexpected.
    You make a good point. I'll keep it in mind and change it in my program.

    I will now be doing the reading into a temporary instance of the class and as you suggested, use a identifier say "-1 \0" for an empty record.

    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. delete record from random access binary file
    By jack jordan in forum C Programming
    Replies: 1
    Last Post: 11-06-2017, 02:08 PM
  2. Binary file - delete record issue
    By ulti-killer in forum C Programming
    Replies: 1
    Last Post: 12-03-2012, 11:29 AM
  3. Deleting Text File Record
    By 01010011 in forum C++ Programming
    Replies: 5
    Last Post: 04-21-2009, 02:46 PM
  4. Deleting / Changing a record in a text file
    By clearrtc in forum C Programming
    Replies: 9
    Last Post: 08-21-2006, 12:09 AM
  5. Deleting a Record from an input file
    By goron350 in forum C++ Programming
    Replies: 5
    Last Post: 06-15-2005, 03:40 PM

Tags for this Thread