473,503 Members | 1,687 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Text encodings

I am using the StreamReader to read in some text from a plain txt file
and then display it in a text box.

When I look at the text file in notepad and my program the text looks
all messed up, when I look at it in wordpad the spacing is correct and
there are no funny charcters.

How can I make sure the right text encoding is used so the file displays
properly ?

any help appreciated.

--
Eps
Nov 16 '05 #1
9 1489
Andy Burchill wrote:
I am using the StreamReader to read in some text from a plain txt
file and then display it in a text box.

When I look at the text file in notepad and my program the text looks
all messed up, when I look at it in wordpad the spacing is correct
and there are no funny charcters.
Save for those encodings, which use preambles that allow them to be
identified (e.g. UTF-8 with BOM or UTF-16), there's no good way to
identify the correct encoding, so applications need to guess. Seems
Notepad is guessing wrong ;-)
How can I make sure the right text encoding is used so the file
displays properly ?


Silly answer: Use the right encoding. If you want to create an
application that supports multiple encodings, you have to provide some
means for the user to select a specific encoding.
Cheers,
--
http://www.joergjooss.de
mailto:ne********@joergjooss.de
Nov 16 '05 #2
Silly answer: Use the right encoding. If you want to create an
application that supports multiple encodings, you have to provide some
means for the user to select a specific encoding.
Cheers,


thanks for that, what is the esiest way to find out which encoding a
text file is using ?, i tried all the availiable encodings in my program
but none displayed properly.

--
Eps
Nov 16 '05 #3
Andy Burchill <bi********@microsoft.com> wrote:
thanks for that, what is the esiest way to find out which encoding a
text file is using ?, i tried all the availiable encodings in my program
but none displayed properly.


There's no guaranteed way to find out which encoding a text file is
using. For instance, any file is a valid code page 1252 file, but that
doesn't mean it's what you want.

Are you absolutely sure it's a plain text file in the first place,
rather than something like a word document?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
Andy Burchill wrote:

thanks for that, what is the esiest way to find out which encoding a
text file is using ?, i tried all the availiable encodings in my
program but none displayed properly.


As I wrote before, at the end of the day it's trial & error with an
editor that allows you to switch between encodings on the fly.

Cheers,
--
http://www.joergjooss.de
mailto:ne********@joergjooss.de
Nov 16 '05 #5

Are you absolutely sure it's a plain text file in the first place,
rather than something like a word document?


I think so, I am currently using the read to end method of the
streamreader, I'll try reading a line at a time to see if this preserves
the original spacing.

I am enjoying c# so but I have a silly question I can't find the answer
to, whats the difference between a class and a namespace ?

--
Eps
Nov 16 '05 #6
Ok just to elaborate a little I am trying to rewrite a java program I
wrote last year, I checked the java source code and found out that for
some reason I was using the UTF8 encoding when saving txt files.

Unfortuantely setting the c# streamreader to use ut8 hasn't solved the
problem, so now I am really stuck. If it makes any difference the java
program used a fileOutputStream wrapped around a BufferedInputStream
wrapped around a OutputStreamWriter.

Any help appreciated.

--
Eps
Nov 16 '05 #7
Andy Burchill <bi********@microsoft.com> wrote:
Are you absolutely sure it's a plain text file in the first place,
rather than something like a word document?
I think so, I am currently using the read to end method of the
streamreader, I'll try reading a line at a time to see if this preserves
the original spacing.


Well, in what way is it "all messed up" at the moment? If the problem
is at the end of each line, it could well be that your Java code was
writing "\n" rather than the "\r\n" that a textbox would want.
I am enjoying c# so but I have a silly question I can't find the answer
to, whats the difference between a class and a namespace ?


A namespace is just a way of disambiguating names - for instance,
there's System.Windows.Forms.Control and System.Web.UI.Control.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #8
Jon Skeet [C# MVP] wrote:
Andy Burchill <bi********@microsoft.com> wrote:
Are you absolutely sure it's a plain text file in the first place,
rather than something like a word document?


I think so, I am currently using the read to end method of the
streamreader, I'll try reading a line at a time to see if this preserves
the original spacing.

Well, in what way is it "all messed up" at the moment? If the problem
is at the end of each line, it could well be that your Java code was
writing "\n" rather than the "\r\n" that a textbox would want.


The spacing was wrong, where it should have started a new line it just
wrapped, some funny non alphabetic or numerical characters here and there.

Sorted the problem out now, using a richTextBox instead, i don't
actually think it was ever a problem with the text encoding at all, just
that the standard textbox can't handle any formatting whatsoever (not
even new lines). Think about it this seems a little obvious, oh well,
you live and learn.

thanks for all your help.

--
Eps
Nov 16 '05 #9
Andy Burchill <bi********@microsoft.com> wrote:
Well, in what way is it "all messed up" at the moment? If the problem
is at the end of each line, it could well be that your Java code was
writing "\n" rather than the "\r\n" that a textbox would want.
The spacing was wrong, where it should have started a new line it just
wrapped, some funny non alphabetic or numerical characters here and there.


Here and there? If it's wherever there should be a newline, that sounds
like your problem.
Sorted the problem out now, using a richTextBox instead, i don't
actually think it was ever a problem with the text encoding at all, just
that the standard textbox can't handle any formatting whatsoever (not
even new lines). Think about it this seems a little obvious, oh well,
you live and learn.


It certainly *can* handle new lines, but it has to be CRLF (\r\n).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #10

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

Similar topics

4
8260
by: Rajorshi | last post by:
Hello! How do I determine the encoding of a text file ? That is, given a text file I want to know the encoding it is in UTF8 or UTF16 or Latin etc. It would be very helpful if you could tell me...
14
4462
by: Job Lot | last post by:
I have tab delimited text file which gets populated on daily basis via automated process. New entry is written at the bottom. I need to create a utility which makes a copy of this file with 10 most...
9
2778
by: Frances | last post by:
at work we switched to UTF-16 encoding and now when I open html files in HomeSite the code is all messed up, and if I turn on "enable non-ANSI file encoding" code looks fine when I open files but I...
4
18967
by: hagai26 | last post by:
I have a hebrew text file, which I want to read in python I don't know which encoding I need to use & how I do that thanks, hagai
10
8011
by: Nikolay Petrov | last post by:
How can I convert DOS cyrillic text to Unicode
29
4832
by: list | last post by:
Hi folks, I am new to Googlegroups. I asked my questions at other forums, since now. I have an important question: I have to check files if they are binary(.bmp, .avi, .jpg) or text(.txt,...
13
3657
by: mario | last post by:
Hello! i stumbled on this situation, that is if I decode some string, below just the empty string, using the mcbs encoding, it succeeds, but if I try to encode it back with the same encoding it...
4
1501
by: =?ISO-8859-1?Q?Nordl=F6w?= | last post by:
How do I efficiently determine which possible encoding(s) a given text is in? Can I use the iconv.h api somehow? Thanks in advance, Nordlöw
3
2631
by: Philip Semanchuk | last post by:
On Nov 9, 2008, at 7:00 PM, News123 wrote: Look under the heading "Standard Encodings": http://docs.python.org/library/codecs.html Note that both the page you found (which appears to be a...
0
7202
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
7086
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
7280
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,...
1
6991
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
7462
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...
0
5578
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 project—planning, coding, testing,...
1
5014
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
1512
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 ...
0
382
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...

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.