Home All Groups Group Topic Archive Search About

Streamreader doesn't read the line properly

Author
14 Jun 2006 3:55 PM
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.

Author
14 Jun 2006 4:01 PM
Mythran
<sweetpota***@yahoo.com> wrote in message
Show quoteHide quote
news:1150300526.470825.146020@y41g2000cwy.googlegroups.com...
> 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
Author
14 Jun 2006 4:08 PM
sweetpotatop
Is there a quick way to read a file with any format?

Thanks again

Mythran wrote:
Show quoteHide quote
> <sweetpota***@yahoo.com> wrote in message
> news:1150300526.470825.146020@y41g2000cwy.googlegroups.com...
> > 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
Author
14 Jun 2006 4:04 PM
CT
Have you tried instantiating the StreamReader class instance using the
constructor that accepts an encoding parameter?
http://msdn2.microsoft.com/en-us/library/ms143456.aspx

--
Carsten Thomsen
Communities - http://community.integratedsolutions.dk
---------
Voodoo Programming: Things programmers do that they know shouldn't work but
they try anyway, and which sometimes actually work, such as recompiling
everything. (Karl Lehenbauer)
---------
<sweetpota***@yahoo.com> wrote in message
Show quoteHide quote
news:1150300526.470825.146020@y41g2000cwy.googlegroups.com...
> 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.
>
Author
14 Jun 2006 5:31 PM
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

CT wrote:
Show quoteHide quote
> Have you tried instantiating the StreamReader class instance using the
> constructor that accepts an encoding parameter?
> http://msdn2.microsoft.com/en-us/library/ms143456.aspx
>
> --
> Carsten Thomsen
> Communities - http://community.integratedsolutions.dk
> ---------
> Voodoo Programming: Things programmers do that they know shouldn't work but
> they try anyway, and which sometimes actually work, such as recompiling
> everything. (Karl Lehenbauer)
> ---------
> <sweetpota***@yahoo.com> wrote in message
> news:1150300526.470825.146020@y41g2000cwy.googlegroups.com...
> > 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.
> >
Author
14 Jun 2006 5:54 PM
Herfried K. Wagner [MVP]
<sweetpota***@yahoo.com> schrieb:
>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.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
14 Jun 2006 5:28 PM
Herfried K. Wagner [MVP]
<sweetpota***@yahoo.com> schrieb:
> 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'.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
14 Jun 2006 5:35 PM
sweetpotatop
Perfect, I get it!!!! Million thanks.


Herfried K. Wagner [MVP] wrote:
Show quoteHide quote
> <sweetpota***@yahoo.com> schrieb:
> > 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'.
>
> --
>  M S   Herfried K. Wagner
> M V P  <URL:http://dotnet.mvps.org/>
>  V B   <URL:http://classicvb.org/petition/>