1
votes

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.

1

1 Answers

1
votes

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.