Streamreader doesn't read the line properly

S

sweetpotatop

Hello,

I have a txt file which has a few lines of special characters:

This is A line from the text file:

¤Ù¦³‡-áŧÐþ®ÎÙ¡©ÌÄð‰äûñ·èãûóÂʬ‡Šœ†£ÜÆÜÑ¥ŽÆþ†ÓÖÁ€À¤ÃÞ˜æ²ÐůêÁ¿ö'÷·Û£¨™üˆ¾©÷±ëªÐ±­'±ä±Ñ¬ˆõÏ ðüŽ¥üŠÙ',,™±¯Ø·‡±¦êŽÍÐÞã¢×®

When I use
sr.ReadLine (where "sr As StreamReader"), it is not giving me this line
at all, it gets cut and the characters are totally different.

But when I try a real text file with normal characters, it reads
everything correctly.

How can I read a file, one line at a time and be able to give me the
correct data in VB.NET?

Thanks in advance. You help would be greatly appreciated.
 
M

Mythran

Hello,

I have a txt file which has a few lines of special characters:

This is A line from the text file:

¤Ù¦³?-áŧÐþ®ÎÙ¡©ÌÄð?äûñ·èãûóÂʬ?So?£ÜÆÜÑ¥ZÆþ?ÓÖÁ?À¤ÃÞ~æ²ÐůêÁ¿ö'÷·Û£¨Tü^¾©÷±ëªÐ±­'±ä±Ñ¬^õÏ
ðüZ¥üSÙ',,T±¯Ø·?±¦êZÍÐÞã¢×®

When I use
sr.ReadLine (where "sr As StreamReader"), it is not giving me this line
at all, it gets cut and the characters are totally different.

But when I try a real text file with normal characters, it reads
everything correctly.

How can I read a file, one line at a time and be able to give me the
correct data in VB.NET?

Thanks in advance. You help would be greatly appreciated.

What you are reading is a binary file, not a text file. The binary data may
contain the ascii values of 13, 10, or 0. All of which (i believe) will
keep the StreamReader from reading the full line...

What you should do is read a byte at a time, up to the first ASCII 13. Then
see what data the byte-array contains.

HTH,
Mythran
 
H

Herfried K. Wagner [MVP]

I have a txt file which has a few lines of special characters:

This is A line from the text file:

¤Ù¦³?-áŧÐþ®ÎÙ¡©ÌÄð?äûñ·èãûóÂʬ?So?£ÜÆÜÑ¥ZÆþ?ÓÖÁ?À¤ÃÞ~æ²ÐůêÁ¿ö'÷·Û£¨Tü^¾©÷±ëªÐ±­'±ä±Ñ¬^õÏ
ðüZ¥üSÙ',,T±¯Ø·?±¦êZÍÐÞã¢×®

When I use
sr.ReadLine (where "sr As StreamReader"), it is not giving me this line
at all, it gets cut and the characters are totally different.

'StreamReader' interprets the file as UTF-8 by default. Maybe the file has
been saved using another encoding such as Windows ANSI with the system's
default code page. Note that you can pass an encoding to 'StreamReader''s
overloaded constructor. To obtain the system's ANSI encoding, use
'System.Text.Encoding.Default'.
 
S

sweetpotatop

Thank you for your feedback. However, when I tried
sr As StreamReader = New StreamReader(sFileName,
System.Text.Encoding.ASCII)

It give me "????....."

Basically they are ASCII between 128 and 255. What should I be using,
or how should I declare it in order to get those characters back?

Thanks in advance.
Wanda
 
S

sweetpotatop

Perfect, I get it!!!! Million thanks.

'StreamReader' interprets the file as UTF-8 by default. Maybe the file has
been saved using another encoding such as Windows ANSI with the system's
default code page. Note that you can pass an encoding to 'StreamReader''s
overloaded constructor. To obtain the system's ANSI encoding, use
'System.Text.Encoding.Default'.
 
H

Herfried K. Wagner [MVP]

Basically they are ASCII between 128 and 255. What should I be using,
or how should I declare it in order to get those characters back?

ASCII is a 7-bit encoding, this means only character codes 0 through 127 are
defined. You may want to try 'System.Text.Encoding.Default' for the
system's ANSI codepage or other encodings in 'System.Text.Encoding'. Note
that 'Encoding.Unicode' means UTF-16.
 

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