StreamReader problem with certain characters

M

myjunkbuster

Howdy,

I have a simple C# program that opens a file for reading a text file,
creates a new file for writing, reads a line from the source file and writes
it to the target file. I did this using StreamReader and StreamWriter. I'm
doing this one line at a time on purpose because I want to strip out certain
things and save results in a different file. In comparing the two files I
saw that the target file had ? char where in the source file it had a -
character.

I looked at that the source file's - character via a hex editor and it has a
hex value of 96. Other - characters in the source file have a hex value of
2D. These 2D guys were written to the target file just fine. So it seems
that StreamReader doesn't know what the hex 96 - character is and is
replacing it with a ? character.

Instead of writing to a file I put in a Console.Write and it too wrote the ?
character to the screen. So it seems to me that the problem is with
StreamReader and not the StreamWriter. I also tried specifying various
encoders on the open like Default, ASCII, UTF7, UTF8 and none allowed
StreamReader to read that character.

So it looks like I'm going to have to do a replace on the hex 96 - character
with the hex 2D - character before I copy the file over. This may work but
perhaps there are other special characters in these files that won't be
processed correctly. So I'm looking for a better solution.

Any suggestions would be most appreciated.

Scott
 
M

myjunkbuster

Found the answer. I'm ashamed to admit that it didn't occur to me right away
that the character that I am having trouble with is actually beyond the
defined range of ASCII (0-127). I know this but it has been years since I
had a need.

Anyway, all I had to do was specify the code page as 1252 or Latin I for
both the StreamReader and StreamWriter.

System.Text.Encoding.GetEncoding(1252)

StreamReader srReadLine = new
StreamReader((System.IO.Stream)File.OpenRead(sSrcFile),System.Text.Encoding.
GetEncoding(1252));
StreamWriter swWriteLine = new
StreamWriter(sTrgFile,false,System.Text.Encoding.GetEncoding(1252));

Scott
 

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