How to create ASCII text file from HS2014 (on RHEL) unicode

I'm a beginner for hs2014

I try to write text file in ASCII format (encode CP874) from HS2014 (run on RHEL)

I try to use

o file:"WNSK\CP874\"
o file:"RSK\CP874\"
o file:"WNSK\THAW\
o file:"WNS"

but output file still in UTF-8 encoding

please advise me

Thk.

Answers

Use translate table:

ClassMethod create(file) As %Status
{
    set stream = ##class(%Stream.FileCharacter).%New()
    set stream.TranslateTable = "CP874"
    set sc = stream.LinkToFile(file)
    quit:$$$ISERR(sc) sc
    
    do stream.WriteLine("Hello")

    quit stream.%Save()
}

Eduard, can you explain the main difference between Sagun's method of translation table setting:

 open file:"RSK\CP874\"

and yours in this very case? The latter is a piece of code of %Stream.FileCharacter.cls which actually sets a table:

 If (table'=0) && (i%CurrFile'="") {
   Set io=$io
   Use i%CurrFile
   $$$SETIO(table) ; -> Do $zutil(96,18,2,table)
   Use io
 }

It seems that it's some other problem, perhaps a bug.

Sagun, if you provide us with a small piece of your code where you open the file, use it, and write it, it would be easier to say something.

It's better to use stream wrappers instead of open/use directly.

I'm not sure what's the correct way to set codepage with open command.

It's better to use stream wrappers instead of open/use directly

Just because of higher abstraction level of streams; nothing special inside: just the same Open/Use/etc COS commands. So, choosing one or another way of working with files can't be the source of errors itself.

By the way, `open file:("NWK\table\")` is a proven and documented way of translation table setting. AFAIK, its only limitation is that it works with Open command only rather than with Use command.

documented way of translation table setting.

Can you please link me to the documentation?

Dear Alexey Maslove

n fileno,seq,resfile,ind,ind2,seq2
s resfile=""
s ind="" f  s ind=$o(^ztonEDI("TXT","OUT-DATA",ind)) q:$g(ind)=""  d
.s fileno="" f  s fileno=$o(^ztonEDI("TXT","OUT-DATA",ind,fileno)) q:$g(fileno)=""  d
..s resfile="/Flatfile/"_fileno_".txt"
..o resfile:"RSK\CP874\"
..u resfile
..s seq="" f  s seq=$o(^ztonEDI("TXT","OUT-DATA",ind,fileno,"SEQ",seq)) q:$g(seq)=""  d
...w $g(^ztonEDI("TXT","OUT-DATA",ind,fileno,"SEQ",seq))_$c(13),$c(10)
..c resfile
.k ^ztonEDI("TXT","OUT-DATA",ind)
q

Sagun, this code would fail with <WRITE> error as after opening the file for reading:

 ..o resfile:"RSK\CP874\"

it tries to write into it:

 ...w $g(^ztonEDI("TXT","OUT-DATA",ind,fileno,"SEQ",seq))_$c(13),$c(10)

What this code needs as the first remedy is a change of the Open command to:

..o resfile:"NWK\CP874\"

... and second thing is to insert a Close command before the correspondent Open, e.g.

..c resfile o resfile:"NWK\CP874\"

even if you are quite sure that the file is closed at the moment of opening. The reason is to avoid the cases when your program have failed with error without closing the file, so its open parameters (at least, the translation table setting) keep unchanged despite of subsequent Open command. Such cases often happen during development / debugging, when the error trap code is completely switched off or greatly simplified.