views:

2036

answers:

3

I am writing an application that must generate a plain Text file with fixed sized columns.

my current code is:

Dim MyFilePath As String = Path & FILE_PREFIX & FileNr & ".TXT"

IO.File.Delete(MyFilePath)

Dim FileStr As New IO.StreamWriter(MyFilePath, False, <ENCODER HERE>)
Do While r.Read
    FileStr.WriteLine(r("TXTLine"))
Loop
FileStr.Close()
r.Close()

My problem is that i have some special characters like: "ñ", "à", etc, and i can't find the right encoding.

  • If i use default, then it replaces "ñ" with 2 characters.
  • If i use ASCII then all special Characters end up as: "?"
  • If i use UTF-8 then all text is ok, but it add a "ÿ" in the first byte of the file.

I need the special characters to be writed in the textfile just as they came in the datareader. And i can't have extra characters added becouse columns are of fixed lenght..

What could i do?

Thanks in advance.

+1  A: 

How are you checking that you got the right encoding?

If you're simply opening the file up in notepad, "Windows-1252" is probably the encoding you want.

Encoding.GetEncoding("Windows-1252")

will give you that.

Note that I notice that you say that some columns are fixed length. Are you writing data to a file for import by another tool? If so, then you should check what encoding that other tool requires, not which tool happens to look pretty in whatever other tool you might use to look at the file with (like notepad).

Lasse V. Karlsen
Worked Perfect!
Burnsys
The file will be read by a third party application, to which i have no access.I am looking at the file with V.exe (an HEX Viewer), and i just need it to be compatible with this: http://www.asciitable.com/. That means that "Ñ" equals Dec: 164 or HEX: F1
Burnsys
"If you're simply opening the file in notepad, Windows-1252 is probably the encoding you want". True if you are in the US or Western Europe, not necessarily true in the rest of the world. Of course those folks tend to be more knowledgable about encodings, so it is sensible advice to hand out.
MarkJ
A: 

Wait, you say that the columns must be of "fixed length". That suggests you are trying to have this file read by another application - are you even sure this second application can read "special" (unicode) characters? Are you sure that the other application is not expecting ASCII only, and simply has a specific code page that handles a few of those special characters using totally different numbers? (interprets high bit ASCII characters in some local language).

David
I am reading the file with an HEX viewer.
Burnsys
A: 

The problem is not that you can't find the right encoding, the problem is you don't know what your SOURCE encoding is. Plain ASCII only really has 256 characters and if your source contains accented characters that aren't in the ASCII palette, you won't be able to write them in the ASCII encoding. End of story.

What you need to be doing is re-examining the communication between the two systems. If you need to have unicode characters, then both the source and destination will need to accept unicode be it in a flat file or in some XML document.

You cannot put a square peg (unicode accented characters) into a round hole (ASCII file).

hova
You can as long as the area of the square is equal to the area of the circle multiplied by 2/pi. ;)
Russ Bradberry