Results 1 to 9 of 9

Thread: need help with FileStream for writing text

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2013
    Posts
    21

    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.

  2. #2
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    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

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Mar 2013
    Posts
    21

    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"

  4. #4
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    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:
    1. Dim writer As New IO.FileStream(filename, IO.FileMode.OpenOrCreate, IO.FileAccess.Write)
    2.         Dim s As String = "Name Surname" ' for example
    3.         Dim b(s.Length - 1) As Byte
    4.         For i = 0 To b.Length - 1
    5.             b(i) = BitConverter.GetBytes(s(i))(0) ' it will always be one byte for a character
    6.         Next
    7.         writer.Write(b, offset, b.Length) ' bytearray, position, length
    8.         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!

  5. #5
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    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

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Mar 2013
    Posts
    21

    Re: need help with FileStream for writing text

    Quote Originally Posted by dunfiddlin View Post
    No it isn't. You wouldn't normally use FileStream for text but it's actually not that difficult.

    vb.net Code:
    1. Dim writer As New IO.FileStream(filename, IO.FileMode.OpenOrCreate, IO.FileAccess.Write)
    2.         Dim s As String = "Name Surname" ' for example
    3.         Dim b(s.Length - 1) As Byte
    4.         For i = 0 To b.Length - 1
    5.             b(i) = BitConverter.GetBytes(s(i))(0) ' it will always be one byte for a character
    6.         Next
    7.         writer.Write(b, offset, b.Length) ' bytearray, position, length
    8.         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:
    1. Dim f(TextBox2.Text.Length - 1) As Byte
    2.             For i = 0 To f.Length - 1
    3.                 f(i) = BitConverter.GetBytes(TextBox2.Text(i))(0)
    4.             Next
    5.             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.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Mar 2013
    Posts
    21

    Re: need help with FileStream for writing text

    Quote Originally Posted by billboy View Post
    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.

  8. #8
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    Re: need help with FileStream for writing text

    you shouldnt have another Dim writer, just one

    post your code

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Mar 2013
    Posts
    21

    Re: need help with FileStream for writing text

    vb.net Code:
    1. Private Sub WriteFile()
    2.  
    3.         Dim writer As New IO.FileStream(filepath, IO.FileMode.OpenOrCreate, IO.FileAccess.Write)
    4.  
    5.         Try
    6.             writer.Position = 10     'dec location of sex 01 male 02 female
    7.             writer.WriteByte(CType(TextBox3.Text, Byte))    ' insert 8 bit value
    8.             writer.Position = 12     'dec location of age
    9.             writer.WriteByte(CType(TextBox4.Text, Byte))    ' insert 8 bit value
    10.         Catch ex As Exception
    11.             '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.
    12.             MsgBox(ex.Message)
    13.         Finally
    14.             writer.Close()
    15.             writer.Dispose()
    16.         End Try
    17.  
    18.     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
  •  



Click Here to Expand Forum to Full Width