1

My Environment: RadStudio XE4 Update1 on Windows7 pro (32bit).

When we want to write file with Encoding, what are the APIs?

I found TStreamWriter.Create(FileName, ..., TEncoding.GetEncoding(...)) as one option. Is this what are used normally?

I also found TStringList.SaveToFile() with Encoding.

I tried to find the way to specify Encoding for FileOpen()/Filewrite(), but did not find the related information.

manlio
  • 18,345
  • 14
  • 76
  • 126
sevenOfNine
  • 1,509
  • 16
  • 37

1 Answers1

1

There is the AssignFile function. From XE it has an optional CodePage parameter that sets the codepage of the output file (see https://stackoverflow.com/a/14243866/3235496).

Also the TStringStream::Create method accepts a TEncoding parameter. While TStringList is line-oriented, the TStringStream class is very useful to read/write string blob (see https://stackoverflow.com/a/6397914/3235496).

These should cover many typical use cases.

FileOpen() / FileWrite() are lower-level file access routines. Embarcadero doesn't encourage the use of the non-native Delphi language file handlers and for normal file operations they suggest AssignFile, Rewrite and Reset instead.

EDIT

Anyway I'd stay with the TStringStream / TStringList solution if possible.

Community
  • 1
  • 1
manlio
  • 18,345
  • 14
  • 76
  • 126
  • 2
    `FileOpen()`/`FileWrite()` are provided by the Delphi RTL (they are in the `SysUtils` unit). `AssignFile()`, `Rewrite()` and `Reset()` are old Pascal-style I/O functions, THOSE are the ones you should not be using anymore, Embarcadero does NOT encourage them. – Remy Lebeau Oct 23 '14 at 22:29
  • @manlio: Thank you very much for you comment. Also the links are helpful to understand. – sevenOfNine Oct 24 '14 at 00:59
  • @Remy Lebeau: Thank you for your comment. I may be missing the point. Do you say, we should not use AssignFile(), Rewrite(), Reset() anymore? – sevenOfNine Oct 24 '14 at 01:01
  • 1
    @sevenOfNine: Yes, that is what I am saying. They are legacy, deprecated, replaced with several other RTL alternatives. – Remy Lebeau Oct 24 '14 at 01:22
  • @RemyLebeau: Then, I may use TStreamWriter / TStringStream not using lower-level file access routines. – sevenOfNine Oct 24 '14 at 01:34
  • @RemyLebeau Of course you are right (and I also prefer other alternatives). Anyway the sentence about `FileOpen`/`FileWrite` comes directly from the last Embarcadero documentation (http://docwiki.embarcadero.com/Libraries/XE7/en/System.SysUtils.FileOpen) – manlio Oct 24 '14 at 07:54