|
-
Mar 22nd, 2013, 08:00 PM
#1
Thread Starter
Junior Member
need help with FileStream for writing text
I am new to using FileStream, it was introduced to me by a member here, dunfiddlin, when I had an issue with a project for a friend.
I am now trying to expand my horizon more with a little program for myself, and trying to figure out the best way to edit a specific area within a file (in this case it is a file I am calling names.db but it is not an actual database file).
I am trying to have it so that it can write a name that someone puts in a TextBox to a certain offset of the .db file. I know how to do this with 8 bit and 16 bit integers, but I cannot figure out how to do this with text. Here is the code that I got the last time I received help on Filestream:
Code:
Private Sub WriteFile()
Dim writer As New IO.FileStream(filepath, IO.FileMode.OpenOrCreate, IO.FileAccess.Write)
Try
writer.Position = 6244
writer.WriteByte(CType(TextBox1.Text, Byte)) ' insert 8 bit value
writer.Position = 6286
Dim b() As Byte = BitConverter.GetBytes(CType(TextBox8.Text, Int16)).ToArray
writer.WriteByte(b(1))
writer.Position = 6287
writer.WriteByte(b(0))
Catch ex As Exception
MsgBox(ex.Message)
Finally
writer.Close()
writer.Dispose()
End Try
End Sub
Does anyone know how to change what I have here to work with text?
I already figured out how to have it read and program the length of the names, just not how to write the names themselves. I thought maybe trying to convert it all to HEX, but I don't quite understand most of what I am finding. I found examples, but they use so many variables and extra numbers that I am not sure what to change.
I have also heard of streamwriter, but I am not sure if this would be compatible with filestream for writing the other parts.
Thank you for your time.
-
Mar 22nd, 2013, 08:07 PM
#2
Frenzied Member
Re: need help with FileStream for writing text
Not sure exactly what your looking for
but the file stream write has a writeline function
writer.writeline("This is a test")
hope that helps
-
Mar 22nd, 2013, 08:18 PM
#3
Thread Starter
Junior Member
Re: need help with FileStream for writing text
I have tried that, but it gives me an error saying "writer.writeline is not a member of System.IO.FileStream"
-
Mar 22nd, 2013, 08:43 PM
#4
Re: need help with FileStream for writing text
No it isn't. You wouldn't normally use FileStream for text but it's actually not that difficult.
vb.net Code:
Dim writer As New IO.FileStream(filename, IO.FileMode.OpenOrCreate, IO.FileAccess.Write)
Dim s As String = "Name Surname" ' for example
Dim b(s.Length - 1) As Byte
For i = 0 To b.Length - 1
b(i) = BitConverter.GetBytes(s(i))(0) ' it will always be one byte for a character
Next
writer.Write(b, offset, b.Length) ' bytearray, position, length
writer.Close()
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Mar 22nd, 2013, 09:14 PM
#5
Frenzied Member
Re: need help with FileStream for writing text
Code:
Private Sub WriteFile()
Dim fs as New FileStream(filename,FileMode.Create,FileAccess.Write)
Dim writer As New StreamWriter(fs)
writer.WriteLine("SomeText")
writer.close()
End Sub
-
Mar 22nd, 2013, 09:22 PM
#6
Thread Starter
Junior Member
Re: need help with FileStream for writing text
 Originally Posted by dunfiddlin
No it isn't. You wouldn't normally use FileStream for text but it's actually not that difficult.
vb.net Code:
Dim writer As New IO.FileStream(filename, IO.FileMode.OpenOrCreate, IO.FileAccess.Write) Dim s As String = "Name Surname" ' for example Dim b(s.Length - 1) As Byte For i = 0 To b.Length - 1 b(i) = BitConverter.GetBytes(s(i))(0) ' it will always be one byte for a character Next writer.Write(b, offset, b.Length) ' bytearray, position, length writer.Close()
Thanks for helping me out again, but I am a little confused about what to change to what. I thought I knew what to do. Here is the code.
vb.net Code:
Dim f(TextBox2.Text.Length - 1) As Byte For i = 0 To f.Length - 1 f(i) = BitConverter.GetBytes(TextBox2.Text(i))(0) Next writer.Write(f, 20, f.Length)
I have a textbox and tried switching what I thought would be correct, but I obviously missed something. I already used b in one of my int16s, so I used f instead. I wasn't sure what to switch the s to, so I tried TextBox2.Text.
Last edited by Keylan; Mar 22nd, 2013 at 09:29 PM.
-
Mar 22nd, 2013, 09:27 PM
#7
Thread Starter
Junior Member
Re: need help with FileStream for writing text
 Originally Posted by billboy
Code:
Private Sub WriteFile()
Dim fs as New FileStream(filename,FileMode.Create,FileAccess.Write)
Dim writer As New StreamWriter(fs)
writer.WriteLine("SomeText")
writer.close()
End Sub
I tried that with my current setup, but having another Dim writer is giving me errors before letting me test. It won't accept FileStream or StreamWriter without errors.
-
Mar 22nd, 2013, 10:08 PM
#8
Frenzied Member
Re: need help with FileStream for writing text
you shouldnt have another Dim writer, just one
post your code
-
Mar 22nd, 2013, 10:35 PM
#9
Thread Starter
Junior Member
Re: need help with FileStream for writing text
vb.net Code:
Private Sub WriteFile() Dim writer As New IO.FileStream(filepath, IO.FileMode.OpenOrCreate, IO.FileAccess.Write) Try writer.Position = 10 'dec location of sex 01 male 02 female writer.WriteByte(CType(TextBox3.Text, Byte)) ' insert 8 bit value writer.Position = 12 'dec location of age writer.WriteByte(CType(TextBox4.Text, Byte)) ' insert 8 bit value Catch ex As Exception 'This is where I need the name to go at dec 20. I have now fields after this, but I don't want to make the code to long for a sample. MsgBox(ex.Message) Finally writer.Close() writer.Dispose() End Try End Sub
There is a small section of my writefile.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|