473,473 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Using Stream objects with encoding

(Type your message here)
Hi,
I want to open a file to read from and then edit and write the text to another file.
I am using StreamReader and StreamWriter for this purpose.
The problem is that i don't know the encoding of the file, because it can be saved at any encoding.
When using the stream objects it is using the UTF-8 encoding, and therefore after editting the file, some characters are omitted.
How can i know the file encoding? or any other solution?

The code i am using:

//Reading from file
StreamReader sReader = new StreamReader(path);
string str = sReader.ReadToEnd();

[..... edditing the string .....]

//writing to file
StreamWriter sStr = new StreamWriter(newpath);
sStr.Write(str);

Regards,
Gaia.

--------------------------------
From: Gaia C

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

<Id>PpWef8nJyUKZ0jfeAZs2LQ==</Id>
Nov 16 '05 #1
3 15429
Hi Gaia,

Unless you know the encoding the file was written in you have to try likely code pages.
The StreamReader assumes UTF-8 by default, but files are written as 8-bit text by default so any extended ascii characters would be lost.

In my case, using æ ø å in any file and reading it as utf-8 would only show the regular ascii characters. To be able to read these characters properly I would have to use something like this:

StreamReader sr = new StreamReader("D:\\test.txt", Encoding.GetEncoding("iso-8859-15"));

Use the most likely encoding in your area.
--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #2
Morten Wennevik <Mo************@hotmail.com> wrote:
Unless you know the encoding the file was written in you have to try
likely code pages.
Indeed - and be aware that you are guessing and could easily be wrong.
IMO it's rarely a good idea to read a text file without knowing the
encoding.
The StreamReader assumes UTF-8 by default, but files are written as
8-bit text by default so any extended ascii characters would be lost.
What do you mean by "files are written as 8-bit text by default"? It
entirely depends on what's being used to write the files. If you use
StreamWriter, it'll use UTF-8 by default.
In my case, using æ ø å in any file and reading it as utf-8 would
only show the regular ascii characters. To be able to read these
characters properly I would have to use something like this:

StreamReader sr = new StreamReader("D:\\test.txt",
Encoding.GetEncoding("iso-8859-15"));

Use the most likely encoding in your area.


Or use Encoding.Default, which is the system's current ANSI code page.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3
On Mon, 6 Sep 2004 10:57:08 +0100, Jon Skeet [C# MVP] <sk***@pobox.com> wrote:
Morten Wennevik <Mo************@hotmail.com> wrote:
Unless you know the encoding the file was written in you have to try
likely code pages.


Indeed - and be aware that you are guessing and could easily be wrong.
IMO it's rarely a good idea to read a text file without knowing the
encoding.
The StreamReader assumes UTF-8 by default, but files are written as
8-bit text by default so any extended ascii characters would be lost.

What do you mean by "files are written as 8-bit text by default"? It
entirely depends on what's being used to write the files. If you use
StreamWriter, it'll use UTF-8 by default.


I was thinking of pure text files created by simple programs like Edit, Notepad or similar, using the code pages of the current system. A bad case of thinking specific scenarios, but writing it vaguely :(
In my case, using æ ø å in any file and reading it as utf-8 would
only show the regular ascii characters. To be able to read these
characters properly I would have to use something like this:

StreamReader sr = new StreamReader("D:\\test.txt",
Encoding.GetEncoding("iso-8859-15"));

Use the most likely encoding in your area.


Or use Encoding.Default, which is the system's current ANSI code page.


That would probably be the most likely encoding :)

--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #4

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

Similar topics

0
by: Chinook | last post by:
Using code objects? =================== As an OO exercise I have a factory pattern that returns class objects that each have an "action" method. ClassObj.action() in turn returns a code object...
5
by: Drew Yallop | last post by:
I read an XML file with a stream reader in VB.Net. When I look at the stream reader output in debug mode (by passing cursor over the stream reader object)the format is a perfect replica of the...
2
by: Tom Gerstner | last post by:
Dear all, I'm trying to program a IE Toolbar similar to the Google Bar in C# using Band Objects. Everything works fine but my TextBox (which I use to search the Internet) is not responding the...
0
by: Ireney Berezniak | last post by:
Hi, I am trying to send an UTF-8 XML string to remote resource via HttpWebRequest using POST method, which means that I need to get the request stream and write my XML string to it. No problems...
2
by: Loane Sharp | last post by:
Hi there I'm downloading data from a remote server to my local disk using Stream.Read() and Stream.Write() (as previously suggested by Ken Tucker in this group). By and large the download and...
2
by: Primera | last post by:
I am building an application that will fix problems we experience with the SMS Advanced Client. I would like to be able to force a Machine Policy Update as you can through the SMS Advanced Client...
11
by: Benny | last post by:
I just wanted to throw the discussion out there on what the best practice people feel is for using large objects in a foreach loop. For example if you are reusing an Image object in a loop like...
0
by: PRITPAL | last post by:
Hi There, I want a code for Saving and Reading formatted text (RTF File) in MS Access using ole Objects, i want to save 20 such records in DB using VB 6.0. Plz Help
1
by: blessonin | last post by:
hi all, i want to know how to read the content of a http posted file using stream reader i am using the folloing line but an error in the last line sayin that " 'System.IO.Stream' cannot be...
0
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,...
0
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...
0
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...
1
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
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...
0
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...
0
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 ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.