Streamreader problem

  • Thread starter Thread starter Jack Russell
  • Start date Start date
J

Jack Russell

I have the following code in VB.net 2003

Dim s As String
Dim sr As StreamReader = New StreamReader("test.txt", New
UTF7Encoding)
s = sr.ReadLine
Debug.Write(s)

If the string read is 1+.03 it converts it to 1.3

If I remove the UTF7encoding option it does not.

Why should the encoding cause a text string to do arithmetic work? and
can I prevent it.

Thanks

Jack Russell
 
I think it's in the UTF encoding.
The '+' may not be a valid charactor, so it ignores it.
Can you avoid UTF7? UTF8 seems to work fine.
 
Ryan said:
I think it's in the UTF encoding.
The '+' may not be a valid charactor, so it ignores it.
Can you avoid UTF7? UTF8 seems to work fine.
You are right, I just discovered that UTF7 regards + as an escape character.

I have files written by the old Fileopen, printline,fileclose method

I use streamreader to read these as it seems much faster than openfile,
lineinput.

It all works fine until I try to read files containing French such as

äüöéèà

The only way I can find to read these is with UTF7, everything else
ignores them.

Any ideas gratefully received.

Jack
 
Try Binary:

Dim sr As New BinaryReader(File.OpenRead("test.txt"))
Do While sr.PeekChar >= 0
Console.Write(Chr(sr.ReadByte))
Loop
sr.Close()

Notice I used chr code to convert bye to character.


--
Thiele Enterprises - The Power Is In Your Hands Now!


--
Ryan said:
I think it's in the UTF encoding.
The '+' may not be a valid charactor, so it ignores it.
Can you avoid UTF7? UTF8 seems to work fine.
You are right, I just discovered that UTF7 regards + as an escape character.

I have files written by the old Fileopen, printline,fileclose method

I use streamreader to read these as it seems much faster than openfile,
lineinput.

It all works fine until I try to read files containing French such as

äüöéèà

The only way I can find to read these is with UTF7, everything else
ignores them.

Any ideas gratefully received.

Jack
 
Ryan said:
Try Binary:

Dim sr As New BinaryReader(File.OpenRead("test.txt"))
Do While sr.PeekChar >= 0
Console.Write(Chr(sr.ReadByte))
Loop
sr.Close()

Notice I used chr code to convert bye to character.
Well that works thanks but with the large files that I am reading it is
horrendously slow which is why I went to streamreader in the first
place!! Any other ideas?
 
Jack said:
Well that works thanks but with the large files that I am reading it is
horrendously slow which is why I went to streamreader in the first
place!! Any other ideas?

Have you tried Encoding.Default instead of UTF7? What encoding was
used to write the file?
 
Are you editing the text? Use a richtext box...

Me.RichTextBox1.LoadFile("Text.exe", RichTextBoxStreamType.PlainText)


--
Thiele Enterprises - The Power Is In Your Hands Now!

--
Ryan said:
Try Binary:

Dim sr As New BinaryReader(File.OpenRead("test.txt"))
Do While sr.PeekChar >= 0
Console.Write(Chr(sr.ReadByte))
Loop
sr.Close()

Notice I used chr code to convert bye to character.
Well that works thanks but with the large files that I am reading it is
horrendously slow which is why I went to streamreader in the first
place!! Any other ideas?
 
Chris said:
Have you tried Encoding.Default instead of UTF7? What encoding was
used to write the file?
I do not know what encoding was used, it was written using the "old" VB
method of file open, printline, file close. I have not been able to find
out what encoding that uses. Just to recap my problem the file can
contain foregin characters such as äüöéèà. If I do not specify any
encoding when I open the streamreader these get lost. If I specify UTF7
they are found but UTF7 treats + as an escape character. I will try
default but ...
 
Jack said:
I do not know what encoding was used, it was written using the "old"
VB method of file open, printline, file close. I have not been able
to find out what encoding that uses.

I suspect you'll find the bits you need in the help at the
System.Text.ASCIIEncoding section, and something to do with Windows-1252 or
ISO 8859-1.

If you look at your file with a hex editor, do the accented characters
correspond to the ones in this table:-
http://en.wikipedia.org/wiki/Windows-1252

?

Also see http://en.wikipedia.org/wiki/ISO_8859-1.

HTH

Andrew
 
Back
Top