473,503 Members | 2,046 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.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 ?
Feb 3 '06 #1
3 3899
"philip" <ph**@philippe.com> schrieb
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

Feb 3 '06 #2
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
"Armin Zingler" <az*******@freenet.de> a écrit dans le message de news:
el**************@TK2MSFTNGP11.phx.gbl...
"philip" <ph**@philippe.com> schrieb
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

Feb 4 '06 #3
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


Feb 4 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
5114
by: Mark Miller | last post by:
I have a char array and when I write it to a file using BinaryWriter the position of the pointer is the size of the array + 1. For example: writing char leaves the pointer at position 26 after...
6
2971
by: ThunderMusic | last post by:
Hi, In my app, I open a file using a FileStream then pass it to a BinaryWriter. I then use the BinaryWriter instance to write to my file. But a problem arose : The file never gets bigger than 1kb....
17
3776
by: Filip Strugar | last post by:
Considering that the BinaryWriter/BinaryReader object closes the underlaying stream upon being gc collected, is the following code correct, and if it is what is the reason preventing BinaryWriter...
9
4631
by: John | last post by:
Hi, I need to write out bits that I receive from another process. These are boolean values. I need there to be 8 bits in every byte. I know I could write these bits out as char's using one bit...
1
6210
by: Claire | last post by:
I'm writing from a serial port into a MemoryStream via a BinaryWriter in my protocol object. After I've filled the memorystream, which is approx 200KB in size, I then pass it up to my datahandler...
3
1786
by: Eugene | last post by:
I'm trying to write a class which uses BinaryWriter as its base but allows for queuing of write requests Public Class QueuedBinaryWriter Inherits BinaryWriter I override all the Write methods...
4
17111
by: David Buchan | last post by:
Hello, I wonder if anyone could help me. I'm using vb.NET and I'd like to read a binary file, byte by byte, and then write to another file (making a duplicate, identical file). I'd then...
9
8146
by: philip | last post by:
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() ...
15
7104
by: Zytan | last post by:
I have installed the Visual C# 2005 Code Snippets, and the snippets for file handling use StreamReader and StreamWriter, instead of System.IO.File.*. VB 2005 code snippets don't use these. Why...
0
7091
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7282
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7342
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
5018
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4680
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3162
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1516
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
741
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
391
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.