473,800 Members | 2,523 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.Re adToEnd() 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 6080

"Max" <no****@notvali d.com> wrote in message
news:%2******** ********@TK2MSF TNGP12.phx.gbl. ..
I'm using StreamReader.Re adToEnd() 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****@notvali d.com> wrote in message
news:%2******** ********@TK2MSF TNGP12.phx.gbl. ..
I'm using StreamReader.Re adToEnd() 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.Enc oding
enc = System.Text.Enc oding.GetEncodi ng(1252)
Dim fs As System.IO.FileS tream = New System.IO.FileS tream(strFilePa th,
FileMode.Open, FileAccess.Read , FileShare.Read)

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

-Max

Think I need an fs.close in there too.
"Eliahu" <br******@hotma il.com> wrote in message
news:eP******** ******@TK2MSFTN GP14.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****@notvali d.com> wrote in message
news:%2******** ********@TK2MSF TNGP12.phx.gbl. ..
I'm using StreamReader.Re adToEnd() 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****@notvali d.com> wrote in message
news:%2******** *******@TK2MSFT NGP14.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.Enc oding
enc = System.Text.Enc oding.GetEncodi ng(1252)
Dim fs As System.IO.FileS tream = New System.IO.FileS tream(strFilePa th,
FileMode.Open, FileAccess.Read , FileShare.Read)

Dim buffer(4096) As Byte
While fs.Read(buffer, 0, 4096)
strFileContents = strFileContents & enc.GetString(b uffer)
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(st rFilePath,
System.Text.Enc oding.GetEncodi ng(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.FileS tream = New System.IO.FileS tream(strFilePa th,
FileMode.Open, FileAccess.Read , FileShare.Read)
Dim bytes As Integer = CInt(fs.Length)
Dim sr As New StreamReader(fs , System.Text.Enc oding.GetEncodi ng(1252),
False, 4096)
Dim sb As New Text.StringBuil der(bytes)
Dim charBuf(4096) As Char
Dim charsRead As Integer = sr.ReadBlock(ch arBuf, 0, 4096)
Do While charsRead > 0
sb.Append(charB uf, 0, charsRead)
charsRead = sr.ReadBlock(ch arBuf, 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.T oString 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
3371
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
8633
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 file path. Declare Auto Function GetShortPathName Lib "kernel32" (ByVal lpszLongPath As
17
30674
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 the same as in C#?
10
5883
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 StreamReader(fs, Encoding.Default);
7
2153
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
888
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
3880
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 forward just gets lost. It is using mysql_real_escape_string, but that doesn't seem to help with the ...
2
3588
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 of how I've tried loading the data along with UTF-8 or iso-8859-1:
0
976
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... Example 1: requestHtml = WebRequest.Create(htmlUri);
0
9550
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10501
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
10273
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...
1
10250
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9085
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 projectplanning, coding, testing, and deploymentwithout 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...
0
6811
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
5469
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
5603
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4149
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

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.