StreamWriter and BinaryWriter

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • philip

    #1

    StreamWriter and BinaryWriter

    If I execute that :

    Dim Temp as string = "This is a text"
    Dim sw As StreamWriter
    Dim fullFileName as string = "c:\text.tx t"
    sw = New StreamWriter(fu llFilename)
    sw.Write(temp)
    sw.Close()

    the resulting file 'Text.txt' has the same length than the string 'temp'.
    Idem if I Use BinayWriter.

    BUT if 'temp' contains others characters than readable character, then the
    result file has not the same length than the string.
    So, if temp contains "ÿØÿá" (that is to say a string concanated with the 4
    characters chr(255) & chr(216) & chr(255) and chr(225)), the file writen is
    8 octets long, and not 4 octets like the string (twice more in the file than
    in the string). Consequently, the copy of the memo field in a file to
    rebuild the 'img' file is wrong and image reconstructed is not visible. In
    Access with the method "open for binary", all is good.

    So I think that I don't use the good method in Visual Studio 2005 to copy
    this string on a file without any change.

    Can someone help me and tell me the methos to do that ?


  • Armin Zingler

    #2
    Re: StreamWriter and BinaryWriter

    "philip" <phil@philippe. com> schrieb[color=blue]
    > If I execute that :
    >
    > Dim Temp as string = "This is a text"
    > Dim sw As StreamWriter
    > Dim fullFileName as string = "c:\text.tx t"
    > sw = New StreamWriter(fu llFilename)
    > sw.Write(temp)
    > sw.Close()
    >
    > the resulting file 'Text.txt' has the same length than the string
    > 'temp'. Idem if I Use BinayWriter.
    >
    > BUT if 'temp' contains others characters than readable character,
    > then the result file has not the same length than the string.
    > So, if temp contains "ÿØÿá" (that is to say a string concanated
    > with the 4 characters chr(255) & chr(216) & chr(255) and chr(225)),
    > the file writen is 8 octets long, and not 4 octets like the string
    > (twice more in the file than in the string). Consequently, the copy
    > of the memo field in a file to rebuild the 'img' file is wrong and
    > image reconstructed is not visible. In Access with the method "open
    > for binary", all is good.
    >
    > So I think that I don't use the good method in Visual Studio 2005 to
    > copy this string on a file without any change.
    >
    > Can someone help me and tell me the methos to do that ?[/color]


    Strings are stored as Unicode characters. Each character occupies 2 bytes.
    If you open the Streamwriter without specifying an encoding, it uses UTF-8
    encoding. This means that some characters are stored as 1 byte per character
    and some as 2 bytes per character. In your example, all characters are
    stored as 2 bytes per character.

    Specify a 1-byte-encoding if you want to do so. Usually
    System.Text.Enc oding.Default is used:

    sw = New StreamWriter(fu llFilename, true, System.Text.Enc oding.Default)

    Be aware that conversion from a 2-byte character set to a 1-byte character
    set can lead to information loss. Thus, you should use exactly the same
    encoding to write the file as it has been used to read it.


    Armin

    Comment

    • philip

      #3
      Re: StreamWriter and BinaryWriter

      With System.Text.Enc oding.Default, that's not works.
      I tried System.Text.Enc oding.ASCII, and for the first time, the file has
      exactly the same length of the string.
      But file resulting is not good, and the image is invisible.
      I used Compare It!, an utility to see the origin file and the file done by
      System.Text.Enc oding.ASCII. Some character are différent, like all non
      conventional character.
      So the first octets of original image
      "ÿØÿá·ëExif··II *········ ····1·!···J· ··2"
      became
      "????·?Exif··II *········ ····1·!···J· ··2"
      So ASCII is not solution. Is there another solution? Is it so difficult to
      copy in a file the exact content os a string ?

      Thanks to help me again.
      Sincerely

      Philip


      "Armin Zingler" <az.nospam@free net.de> a écrit dans le message de news:
      elL4uIOKGHA.387 6@TK2MSFTNGP11. phx.gbl...[color=blue]
      > "philip" <phil@philippe. com> schrieb[color=green]
      >> If I execute that :
      >>
      >> Dim Temp as string = "This is a text"
      >> Dim sw As StreamWriter
      >> Dim fullFileName as string = "c:\text.tx t"
      >> sw = New StreamWriter(fu llFilename)
      >> sw.Write(temp)
      >> sw.Close()
      >>
      >> the resulting file 'Text.txt' has the same length than the string
      >> 'temp'. Idem if I Use BinayWriter.
      >>
      >> BUT if 'temp' contains others characters than readable character,
      >> then the result file has not the same length than the string.
      >> So, if temp contains "ÿØÿá" (that is to say a string concanated
      >> with the 4 characters chr(255) & chr(216) & chr(255) and chr(225)),
      >> the file writen is 8 octets long, and not 4 octets like the string
      >> (twice more in the file than in the string). Consequently, the copy
      >> of the memo field in a file to rebuild the 'img' file is wrong and
      >> image reconstructed is not visible. In Access with the method "open
      >> for binary", all is good.
      >>
      >> So I think that I don't use the good method in Visual Studio 2005 to
      >> copy this string on a file without any change.
      >>
      >> Can someone help me and tell me the methos to do that ?[/color]
      >
      >
      > Strings are stored as Unicode characters. Each character occupies 2 bytes.
      > If you open the Streamwriter without specifying an encoding, it uses UTF-8
      > encoding. This means that some characters are stored as 1 byte per
      > character and some as 2 bytes per character. In your example, all
      > characters are stored as 2 bytes per character.
      >
      > Specify a 1-byte-encoding if you want to do so. Usually
      > System.Text.Enc oding.Default is used:
      >
      > sw = New StreamWriter(fu llFilename, true, System.Text.Enc oding.Default)
      >
      > Be aware that conversion from a 2-byte character set to a 1-byte character
      > set can lead to information loss. Thus, you should use exactly the same
      > encoding to write the file as it has been used to read it.
      >
      >
      > Armin[/color]


      Comment

      • philip

        #4
        Re: StreamWriter and BinaryWriter

        Here is some lines of code than I wrote. You can copy/paste theis code as
        code of form1 in a new project.
        My problem is this one :
        I try to write in a file a serie of bytes.
        BUT some bytes written in file are not the sent bytes.
        Copy and paste the following lines to observe my problem.
        What can I do to resolve problem ?
        Only System.Text.Enc oding.ASCII write the same number of bytes, but not the
        good bytes.
        Someone can help me. Thanks by advance.

        Public Class Form1
        Dim TextBox1 As New TextBox
        Dim TextBox2 As New TextBox
        Dim Label1 As New Label
        Dim Label2 As New Label
        Dim Label3 As New Label
        Dim Label4 As New Label

        Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
        System.EventArg s) Handles MyBase.Load
        Me.Size = New System.Drawing. Size(800, 500)

        Label1.Location = New System.Drawing. Size(20, 13)
        Label1.Text = "Data to write"
        Me.Controls.Add (Label1)

        Label2.Location = New System.Drawing. Size(20, 40)
        Label2.Text = "Data written"
        Me.Controls.Add (Label2)

        Label3.Location = New System.Drawing. Size(350, 13)
        'Label3.Text = "Data written"
        Me.Controls.Add (Label3)

        Label4.Location = New System.Drawing. Size(350, 40)
        'Label4.Text = "Data written"
        Me.Controls.Add (Label4)

        TextBox1.Size = New System.Drawing. Size(200, 20)
        TextBox1.Locati on = New System.Drawing. Size(120, 13)
        Me.Controls.Add (TextBox1)

        TextBox2.Size = New System.Drawing. Size(200, 20)
        TextBox2.Locati on = New System.Drawing. Size(120, 40)
        Me.Controls.Add (TextBox2)

        Dim button1 As New Button
        button1.Text = "BinaryWrit er encoding ASCII"
        button1.Size = New System.Drawing. Size(200, 20)
        button1.Locatio n = New System.Drawing. Size(550, 13)
        AddHandler button1.Click, AddressOf Button1_Click
        Me.Controls.Add (button1)

        End Sub
        Protected Sub Button1_Click(B yVal sender As Object, ByVal e As
        System.EventArg s)
        Dim Temp As String = "ÿØÿá"
        TextBox1.Text = Temp
        Label3.Text = "Length to write : " & Temp.Length.ToS tring & "
        octets)"
        Dim fullFileName As String = "c:\convert ed music\test1.jpg "

        Dim sw As System.IO.Strea mWriter
        sw = New System.IO.Strea mWriter(fullFil eName, False,
        System.Text.Enc oding.ASCII)
        sw.Write(Temp)
        sw.Close()

        Dim sr As System.IO.Strea mReader
        sr = New System.IO.Strea mReader(fullFil eName,
        System.Text.Enc oding.ASCII)
        Do While Not sr.EndOfStream
        TextBox2.Text = TextBox2.Text & Chr(sr.Read())
        Loop
        Label4.Text = "Length written : " & TextBox2.Text.L ength.ToString &
        " octets)"
        sr.Close()
        End Sub
        End Class




        Comment

        Working...