file binary problem editing

student101

Member
Joined
Dec 22, 2007
Messages
10
Programming Experience
3-5
hi , i wanna write an application , it holds some data into a .dat file .
i wrote it with structure !
i can add some data into it but i don' know how to edit it !
for example , we have this data into a .dat file :
jack
15
carlos
19
imoa
16
so if i want to edit ( carlos , 19 ) . how cn i do that ?
this is my code :

VB.NET:
Imports Microsoft.VisualBasic.FileSystem
    Structure Person
        Public Name As String
        Public Age As Long
        Public mob As Boolean
    End Structure
    Dim APerson As New Person()
    Dim fr As Integer = FreeFile()

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        FileOpen(fr, "D:\data.dat", OpenMode.Binary, OpenAccess.ReadWrite, OpenShare.LockReadWrite)
        APerson.Name = TextBox1.Text
        APerson.Age = TextBox2.Text
        FilePut(fr, APerson, LOF(fr) + 1)
        FileClose(fr)
    End Sub
this code is for adding ( appending ) some data into it .
& now i wanna edit a specific part of this code !!

someone helps me
thanks...
 
Last edited by a moderator:
also i have to add this portion that i can add to every where that i want in this file with this code but it causes error when i want to read the file

VB.NET:
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        FileOpen(fr, "D:\data.dat", OpenMode.Binary, OpenAccess.ReadWrite, OpenShare.LockReadWrite)
        Dim temp As String = 1

        APerson.Name = "AAAA"
        APerson.Age = 333


        Seek(fr, 30)
        FilePut(fr, APerson, fr)

        FileClose(fr)

    End Sub
 
Last edited by a moderator:
soooooo what?
please answer me if ya can !
Having read thousands and thousands of posts, like cjard and I, one come to appreciate a well formatted post. It really eases reading when you can distinguish plain text, code, quotes etc at a glance. There are probably different brain channels that processes the different types of information and it's just harder having to manually sort it all out. It's the same reason we have the nice code editor in Visual Studio with color formatting and indentation etc. Who cares? If you don't care about submitting a proper post why would we care about replying to it?
 
hi , thanks for your help
i tried to upgrade my knowledge
i want edit a binary file & read it like records.
when i want go to specific position of file & write something more than limited area it removes others blocks .
take a look at this code
VB.NET:
 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim file_name As String = "C:\lol.dat"

        Dim fs As New FileStream(file_name, FileMode.Append)
        Dim binary_writer As New BinaryWriter(fs)
        binary_writer.Write(TextBox1.Text)
        binary_writer.Write(TextBox2.Text)
        binary_writer.Close()
        fs.Close()

    End Sub

&
this portion of code is for viewing file contents
VB.NET:
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim file_name As String = "C:\lol.dat"
        Dim fs As New FileStream(file_name, FileMode.Open)
        Dim binary_writer As New BinaryReader(fs)
        Dim binary_writer2 As New BinaryWriter(fs)

        Dim l As Integer


        While fs.Position > -1
  

            MsgBox(binary_writer.ReadString)

        End While

        binary_writer.Close()
 binary_writer2.Close()
        fs.Close()

i want edit this binary file , suppose if i want place MsgBox(binary_writer.ReadString) into a textbox , so how can i edit it !? how can i found where in is it ?
VB.NET:
        If binary_writer.ReadString = "VALUE" Then
            fs.Seek(-3, SeekOrigin.Current)
            binary_writer2.Write("vbf")
            Exit While

        End If
if i found my favorite value , i don't know how many chars should i back
fs.Seek(-3, SeekOrigin.Current) & if i insert something more than limited , it removes my next blocks , hope you will understand what i mean ,
some please give me a peace of code to edit binary file , or correct my wrongs ,
thanks
 
Ummm.. If you want to insert into the middle of a binary file and extend the file, you'll have to rewrite the entire tail of the file. If this file is small enough, I would definitely recommend reading it into memory using a sensible data structure like List(Of Byte())
If the file is too large to read into memory.. Um, what on earth are you doing editing a file like that (i wouldnt, and i'm a very experienced programmer) - i'd definitely find a better way of storing this data for manipulation.
 
Back
Top