I use C++ Builder to create my cross-platform application. In the app, I will get file name/path by open file dialog. In Windows, it's no problem to take care the unicode string. (ex. "C:\測試") In mac OS X, I can get correct string from UnicodeString. But I can't find a good method to convert it to char array and use "fopen" to open the file correctly. I tried to assign the UnicodeString to AnsiString directly but it became "C:\??". Because "fopen" only accepts "char*" and UnicodeString can only export "char16*", I need to convert it to char for "fopen". Any idea?
Just because fopen()
takes a char*
does not mean you should give it an ANSI string. POSIX APIs on OSX accept UTF-8 encoded filenames, so use UTF8String
instead of AnsiString
. A char*
can point to a UTF-8 string.
Otherwise, don't use fopen()
directly. Use the RTL's own functions instead, like FileCreate()
/FileOpen()
in System.SysUtils.hpp
unit, or the TFileStream
class in System.Classes.hpp
unit. Let the RTL decide internally how to interact with platform APIs for you.