StreamWriter and BinaryWriter

P

philip

If I execute that :

Dim Temp as string = "This is a text"
Dim sw As StreamWriter
Dim fullFileName as string = "c:\text.txt"
sw = New StreamWriter(fullFilename)
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 ?
 
A

Armin Zingler

philip said:
If I execute that :

Dim Temp as string = "This is a text"
Dim sw As StreamWriter
Dim fullFileName as string = "c:\text.txt"
sw = New StreamWriter(fullFilename)
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 ?


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.Encoding.Default is used:

sw = New StreamWriter(fullFilename, true, System.Text.Encoding.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
 
P

philip

With System.Text.Encoding.Default, that's not works.
I tried System.Text.Encoding.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.Encoding.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
 
P

philip

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.Encoding.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(ByVal sender As System.Object, ByVal e As
System.EventArgs) 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.Location = New System.Drawing.Size(120, 13)
Me.Controls.Add(TextBox1)

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

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

End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs)
Dim Temp As String = "ÿØÿá"
TextBox1.Text = Temp
Label3.Text = "Length to write : " & Temp.Length.ToString & "
octets)"
Dim fullFileName As String = "c:\converted music\test1.jpg"

Dim sw As System.IO.StreamWriter
sw = New System.IO.StreamWriter(fullFileName, False,
System.Text.Encoding.ASCII)
sw.Write(Temp)
sw.Close()

Dim sr As System.IO.StreamReader
sr = New System.IO.StreamReader(fullFileName,
System.Text.Encoding.ASCII)
Do While Not sr.EndOfStream
TextBox2.Text = TextBox2.Text & Chr(sr.Read())
Loop
Label4.Text = "Length written : " & TextBox2.Text.Length.ToString &
" octets)"
sr.Close()
End Sub
End Class
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top