473,516 Members | 2,956 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

StreamReader doesn't pick up special characters

Max
I'm using StreamReader.ReadToEnd() to populate and string from a file and
then display it as a literal on my web site. The problem is I'm losing all
the special characters like ,, and that originates in the file I'm
reading. Do I have to tell streamreader what characterset I'm using or
something?

-Max
Nov 18 '05 #1
4 6058

"Max" <no****@notvalid.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
I'm using StreamReader.ReadToEnd() to populate and string from a file and
then display it as a literal on my web site. The problem is I'm losing all
the special characters like ,, and that originates in the file I'm
reading. Do I have to tell streamreader what characterset I'm using or
something?

Yes.

http://msdn.microsoft.com/library/de...ctortopic4.asp

David
Nov 18 '05 #2
when using Unicode all those problems should disappear. what coding do you
use for your file? you should use the same one when you read your file.

"Max" <no****@notvalid.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
I'm using StreamReader.ReadToEnd() to populate and string from a file and
then display it as a literal on my web site. The problem is I'm losing all
the special characters like ,, and that originates in the file I'm
reading. Do I have to tell streamreader what characterset I'm using or
something?

-Max

Nov 18 '05 #3
Max
This is what I came up with, adding the latin code now displays chars in
Norway, and the English versions seem to be unaffected. I'm reading them in
using FileStream now, instead of StreamReader.

If anyone has anyway to improve performance, please correct me -- I need the
best performance out of reading these text files, since they are read in and
displayed to the page per each page load.

Dim strFileContents As String
Dim enc As System.Text.Encoding
enc = System.Text.Encoding.GetEncoding(1252)
Dim fs As System.IO.FileStream = New System.IO.FileStream(strFilePath,
FileMode.Open, FileAccess.Read, FileShare.Read)

Dim buffer(4096) As Byte
While fs.Read(buffer, 0, 4096)
strFileContents = strFileContents & enc.GetString(buffer)
End While

-Max

Think I need an fs.close in there too.
"Eliahu" <br******@hotmail.com> wrote in message
news:eP**************@TK2MSFTNGP14.phx.gbl...
when using Unicode all those problems should disappear. what coding do you
use for your file? you should use the same one when you read your file.

"Max" <no****@notvalid.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
I'm using StreamReader.ReadToEnd() to populate and string from a file and
then display it as a literal on my web site. The problem is I'm losing
all the special characters like ,, and that originates in the file
I'm reading. Do I have to tell streamreader what characterset I'm using
or something?

-Max


Nov 18 '05 #4

"Max" <no****@notvalid.com> wrote in message
news:%2***************@TK2MSFTNGP14.phx.gbl...
This is what I came up with, adding the latin code now displays chars in
Norway, and the English versions seem to be unaffected. I'm reading them
in using FileStream now, instead of StreamReader.

If anyone has anyway to improve performance, please correct me -- I need
the best performance out of reading these text files, since they are read
in and displayed to the page per each page load.

Dim strFileContents As String
Dim enc As System.Text.Encoding
enc = System.Text.Encoding.GetEncoding(1252)
Dim fs As System.IO.FileStream = New System.IO.FileStream(strFilePath,
FileMode.Open, FileAccess.Read, FileShare.Read)

Dim buffer(4096) As Byte
While fs.Read(buffer, 0, 4096)
strFileContents = strFileContents & enc.GetString(buffer)
End While


Yuck,

All that string concatenation will kill you.

Try this instead, it's short and faster.

Dim strFilePath As String
Dim sr As New StreamReader(strFilePath,
System.Text.Encoding.GetEncoding(1252))
Dim strFileContents As String = sr.ReadToEnd
sr.Close()

For large files you can improve performance a little more if you know how
many bytes per char your encoding has (1252 Latin 1 is a single-byte
encoding), and how large the file is.

Dim fs As System.IO.FileStream = New System.IO.FileStream(strFilePath,
FileMode.Open, FileAccess.Read, FileShare.Read)
Dim bytes As Integer = CInt(fs.Length)
Dim sr As New StreamReader(fs, System.Text.Encoding.GetEncoding(1252),
False, 4096)
Dim sb As New Text.StringBuilder(bytes)
Dim charBuf(4096) As Char
Dim charsRead As Integer = sr.ReadBlock(charBuf, 0, 4096)
Do While charsRead > 0
sb.Append(charBuf, 0, charsRead)
charsRead = sr.ReadBlock(charBuf, 0, 4096)
Loop
sr.Close()
Dim strFileContents As String = sb.ToString
The StringBuilder allows you to pre-allocate the char array for the string.
Then StringBuilder.ToString returns a string which shares that char buffer.

David
Nov 18 '05 #5

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

Similar topics

1
3355
by: Henry | last post by:
Hi, reading a textfile with the ReadLine method of a Streamreader objects the resulting string does not contain the special characters from the source file: e.g. Paragraph § and all the umlauts are missing. Is there a work around or solution for this problem? Thanks in advance --
5
8602
by: Sakharam Phapale | last post by:
Hi All, I am using an API function, which takes file path as an input. When file path contains special characters (@,#,$,%,&,^, etc), API function gives an error as "Unable to open input file". Same file path containing special characters works fine in one machine, but doesn't work in other. I am using following API function to get short...
17
30634
by: Carl Mercier | last post by:
Hi, Is it possible to use special characters like \n or \t in a VB.NET string, just like in C#? My guess is NO, but maybe there's something I don't know. If it's not possible, does anybody know of a VB.NET function (somebody must have coded this already) that will interpret strings containings those special characters, and handle them...
10
5796
by: jcsnippets.atspace.com | last post by:
Hi everybody, I'm trying to read a file containing a list of addresses. Most of these addresses are based in Germany, so there are a lot of names which have special characters like Mnchen and the ever-present "Strae". The code is as follows: FileStream fs = new FileStream(fi.FullName, FileMode.Open); StreamReader sr = new...
7
2137
by: sweetpotatop | last post by:
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
1
881
by: Li Pang | last post by:
Hi, I read a text file line by line, and put the value into a arraylist. But some special characters lost after asignment. Is there any solution to avoid this? Thanks in advance
6
3853
by: TheRealDan | last post by:
Hi all. I'm having a problem with a special characters. I have php script that reads from an xml file and writes to a mysql db. It's a script called phptunest that I found on the net, although the original website for the author appears to be gone. It works really nicely except when it hits special characters. Everything from a sp char...
2
3564
by: matech | last post by:
I have a problem with uploading special characters from excel files to mysql 5. It doesn't matter if I use UTF-8 or iso-8859-1 when uploading the trademark symbol. htmlspecialchars() or htmletities() doesn't help? the database doesn't show the data in the field but replaces it with the binary/Image information. the following are examples...
0
964
by: themillenium | last post by:
Hello! I am trying to get an html page of urls containing special characters as %21,... "%21" stands for "!",.. e.g.: http://en.wikipedia.org/wiki/Joan_R%C3%B6ell (this page was picked my random) And I need these sites, and there are many sites containing these special characters... I tried many variants of getting an html page......
0
7276
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, well explore What is ONU, What Is Router, ONU & Routers main...
0
7182
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7408
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. ...
0
7581
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...
1
7142
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5110
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...
0
3259
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1624
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 we have to send another system
0
488
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.