Creating Plain Text Files

  • Thread starter Thread starter Marc Scirri
  • Start date Start date
M

Marc Scirri

I am currently using a stream writer object to write a text file in C#. One of the employee names in the file has a tilde over the "E" and when written to this file characters are added at the end of the name.

The name is "FORTÉ" and here is what happens when I set the Encoding to different values:

ACSII - FORTÃ?
UNICODE - FORTÃ?
UTF8 - FORT?
UTF7 - FORT+AMk-


If you set the encoding to System.Text.Encoding.GetEncoding("windows-1252") then the text will appear as it should.
 
Marc Scirri said:
I am currently using a stream writer object to write a text file in
C#. One of the employee names in the file has a tilde over the "E"
and when written to this file characters are added at the end of the
name.

The name is "FORTÉ" and here is what happens when I set the Encoding
to different values:

ACSII - FORTÃ?

There's no E-acute in ASCII, hence the problem there.
UNICODE - FORTÃ?
UTF8 - FORT?
UTF7 - FORT+AMk-

Each of those should work fine if you read them with something which
supports the appropriate encoding.
If you set the encoding to
System.Text.Encoding.GetEncoding("windows-1252") then the text will
appear as it should.

What's going to read the file after you've written it? That's what you
need to know in order to decide which encoding to use.
 
Marc,
In addition to Jon's comments, you can use Encoding.Default to write the
text file in the encoding for the system's current ANSI code page (aka
Windows Control Panel Regional settings).

StreamWriter writer = new StreamWriter("myfile.txt", False,
Encoding.Default);

Hope this helps
Jay

I am currently using a stream writer object to write a text file in C#. One
of the employee names in the file has a tilde over the "E" and when written
to this file characters are added at the end of the name.

The name is "FORTÉ" and here is what happens when I set the Encoding to
different values:

ACSII - FORTÃ?
UNICODE - FORTÃ?
UTF8 - FORT?
UTF7 - FORT+AMk-


If you set the encoding to System.Text.Encoding.GetEncoding("windows-1252")
then the text will appear as it should.
 
Back
Top