473,657 Members | 2,409 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.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 ?
Feb 3 '06 #1
3 3910
"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.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 ?

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

Feb 3 '06 #2
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*******@free net.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.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 ?

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

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.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


Feb 4 '06 #4

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

Similar topics

3
5135
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 starting at position 0. I thought that char was 2 bytes, but this makes it seem as though it is just 1 when I write to a file. Why is this? I imagine the extra bit is just a null bit (correct me if I'm wrong). I don't know if this helps but when I...
6
2994
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. The code calls the bw.write(TheValue), but nothing is written after 1kb. I'm I missing something? Here's the way I create my FileStream and BinaryWriter : Filename = System.Environment.CurrentDirectory() & "\msec.dat"
17
3794
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 object garbage collection after the WriteSomething method is executed? ---- using System; using System.IO;
9
4642
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 per byte, but that would be space-inefficient. I'm using od (octal dump) to look at a file produced by calling ..Write(true) and .Write(false) and it looks like it writes out 4 bytes per boolean value.
1
6236
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 object where I plan to create a BinaryReader to extract the data and format it ready for a database insert. At the moment I close the binarywriter earlier than my call to the datahandler object, and Ive just found out that closing the binarywriter...
3
1797
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 like so: Public Overloads Overrides Sub Write(ByVal Value As Byte) m_Queue.Enqueue(New WriteRequest(MyBase.BaseStream.Position, Value)) End Sub 'same for all other Write variants
4
17125
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 like to modify the program to take the integer value of each byte and say, add or subtract an integer from it, with the result still being an integer from 0 to 255. Then write another file that is
9
8156
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() the resulting file 'Text.txt' has the same length than the string 'temp'.
15
7122
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 are they suggesting the use of these classes instead of the fundamental File methods? Are they better? I thought code snippets were supposed show things in the easy / simple way. What do you guys think? What should a beginner learn to do when...
0
8395
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8826
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8732
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7330
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6166
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4155
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4306
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.