views:

1061

answers:

1

Hi everyone.

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

+3  A: 

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);
Roddy
Good answer. Thanks.Yes, I was asking about simple text files.My main goal was to implement unicode logging so I ended up writing simple template class which opens file with selected type of encoding (UTF-8, UTF-16 BE/LE) and then just outputs with FileWrite as they come in.But for a one time write action, your option seems the best.
Andrew