1
votes

I have just switched from Builder 6 to Builder 2009 and have a question.

How can I write unicode string to a file?

TBytes Preamble1 = TEncoding::Unicode->GetPreamble();
UnicodeString str1("string1");
int len = TEncoding::Unicode->GetByteCount(str1);

FileWrite( iFile,&Preamble1[0],Preamble1.Length );

FileWrite( iFile,str1.c_str(),len );

This is what I am doing now but I guess there should be some native way.
Btw, is it OK to get Preamble once and assume that during the application lifetime it does not change? From available documentation for UnicodeString it seems that it is always UTF-16 LE

1
Doesn't CodeGear C++ support the standard C++ libraries? Why don't you use std::wstring and std::ofstream to do the job? - Sahas
Can you, please, provide a working example? I have never used unicode stl functions, only regular ones. - Andrew

1 Answers

3
votes

My first question would be "What kind of file"?

Assuming it's a text file rather than binary, what kind of encoding do you want on the output? UTF-8 is usually a good choice, because it's supported by stuff like notepad, and for normal "Latin" characters, there's no overhead.

Personally, to write 'simple' text files, I just add strings to a TStringList and then use SaveToFile method.

TStringList *list = new TStringList;
list->Add(str1);
list->Add(str2);
...
list->SaveToFile(filename, TEncoding::UTF8);