Delete records in binary file?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Delete records in binary file?

  1. #1
    Join Date
    Sep 2007
    Posts
    195

    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/fid4FB0...D1D0C61.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
    Join Date
    Jul 2012
    Posts
    90

    Re: Delete records in binary file?

    I looked up the code that you have referenced in your link. Then I looked up the code for the implementation of the StructFile class referenced in it. Unfortunately, there is no easy way to do what you are asking. The StructFile class is simple sequential access of a binary file. It can write new structures to the end of the file, and read structures out of the file, starting at the beginning, in a sequential manner. There is no concept of random access in any of it.

    Now to answer your question...

    To delete the ith entry out of the file, you need to:

    1. Create a temporary StructFile.
    2. Read structures from the original file, writing each into the temporary file until you reach the ith one.
    3. Do not write the ith structure, continue reading the rest of the structures and writing them to the temporary file until you reach EOF for the original file.
    4. Delete the original file.
    5. Rename the temporary file using the name of the original file.

    Sequential file access is very simple to code, and very efficient at what it is designed for. What it does well is create a file and read/write sequentially from/to it. When you need to access a particular record (or object) in the file (for read/write/update/delete) is where things get exponentially more complicated.
    Last edited by CGKevin; October 3rd, 2012 at 02:44 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center