#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2010
    Posts
    104
    Rep Power
    4

    Delete records in binary file?


    suppose I have the code was inserting records into the file c: \ \ test.dat see the code http://www.koders.com/csharp/fid4FB06ED9F7F6AA6BB805F9152B3DB29BBD1D0C61.aspx?s = CDEF% 3Afile

    ith now want to delete the records in the file c: \ \ test.dat I can copy the code
    btnAppend_Click to delete sample letter how i
    code:


    'Add items
    private void btnAppend_Click (object sender, System.EventArgs e)
    {
    ....
    sf.Open (System.IO.FileMode.Append, System.IO.FileAccess.Write, System.IO.FileShare.None);???
    ...

    }
    'Delete the ith sample
    private void btnDelete_Click (object sender, System.EventArgs e)
    {
    ....
    sf.Open (System.IO.FileMode.Append, System.IO.FileAccess.Write, System.IO.FileShare.None);???
    ...

    }
  2. #2
  3. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    Nothing is 'deleted' from the middle of a file unless you recopy the file. You could overwrite the data (though the OS/disk driver could decide to do a copy anyway since it is easier to work on pages at a time), but to reclaim the space you will need to rewrite the file.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,175
    Rep Power
    2222
    As I recall dBase III to have done it, when you deleted a record from the file, simply marked it as deleted. The record was still there, just ignored. And then the user could issue a PACK command which removed all deleted records in roughly this fashion:
    1. Open the file for read and open a file with a temporary-file name for write.
    2. Copy the contents of the database file into the temporary file, header first and then record by record. However, if the record is marked as being deleted, you don't copy it.
    3. When the copying is complete, close both files. Delete the original database file and rename the temporary file to the name of the original database file.

    I can imagine doing it with just one file, but that would be nightmarish.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2010
    Posts
    104
    Rep Power
    4
    have you got for example ? add, delete, edit, ... struct binary record form and load the data into the listview? I'm using C # 2005, if you share yourself with, thank you
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,175
    Rep Power
    2222
    No, I don't have. I haven't played with files much in C# yet, but rather have done so quite a bit in C and C++. I haven't played with dBase III file formats since around 1990, when I wrote a Pascal program to extract data from a DBF file rather than have to learn dBase; one of our SKs made First Class because of the resultant hard-copy study guide. I can still describe conceptually what you would do and you should be able to translate that into code.

    We already discussed one method for deleting a record. Another approach, albeit an inefficient one, could be to always do the copy-and-rename-leaving-out-the-record-to-be-deleted bit. And if C# doesn't have a "temp-file name" function, then you could just fake that by using a name that you know you won't be using otherwise.

    To add, simply open the database file for append and write the new record to it.

    To edit, you would need to play with the file pointer. Your File object keeps track of the current position in the file from which the next byte will be read or written; every read or write advances the file pointer. In C, you can manipulate the file pointer with ftell(), which tells you where the file pointer is, and fseek(), which repositions the file pointer. In C#, the File object should have methods that do the same thing; it's up to you to research what they are.

    So, to edit a record, first you open the file for read-and-write. Then you find the record in the file and use ftell()'s C# equivalent to obtain and save the record's position in the file. Read the record from the file into a buffer and modify it. Now use fseek's C# equivalent to re-position the file pointer to the records location in the file and write the modified record to the file.

    Simple as that.

IMN logo majestic logo threadwatch logo seochat tools logo