<p>I'm looking to the equivalent of Windows <a href="http://msdn.microsoft.com/fr-fr/library/yeby3zcb.aspx" rel="noreferrer"><code>_wfopen()</code></a> under Mac OS X. Any idea?</p>

<p>I need this in order to port a Windows library that uses <code>wchar*</code> for its File interface. As this is intended to be a cross-platform library, I am unable to rely on how the client application will get the file path and give it to the library.</p>

[{'answer_id': 12367, 'author': 'Greg Hurlman', 'author_id': 35, 'author_profile': 'https://Stackoverflow.com/users/35', 'pm_score': 0, 'selected': False, 'text': "<p>If you're using Cocoa it's fairly easy with NSString. Just load the UTF16 data in using -initWithBytes:length:encoding: (or perhaps -initWithCString:encoding:) and then get a UTF8 version by calling UTF8String on the result.  Then, just call fopen with your new UTF8 string as the param.</p>\n\n<p>You can definitely call fopen with a UTF-8 string, regardless of language - can't help with C++ on OSX though - sorry.</p>\n"}, {'answer_id': 13562, 'author': 'Chris Hanson', 'author_id': 714, 'author_profile': 'https://Stackoverflow.com/users/714', 'pm_score': 3, 'selected': False, 'text': '<p>You just want to open a file handle using a path that may contain Unicode characters, right?  Just pass the path in <em>filesystem representation</em> to <code>fopen</code>.</p>\n\n<ul>\n<li><p>If the path came from the stock Mac OS X frameworks (for example, an Open panel whether Carbon or Cocoa), you won\'t need to do any conversion on it and will be able to use it as-is.</p></li>\n<li><p>If you\'re generating part of the path yourself, you should create a CFStringRef from your path and then get that in filesystem representation to pass to POSIX APIs like <code>open</code> or <code>fopen</code>.</p></li>\n</ul>\n\n<p>Generally speaking, you won\'t have to do a lot of that for most applications.  For example, many applications may have auxiliary data files stored the user\'s Application Support directory, but as long as the names of those files are ASCII, and you use standard Mac OS X APIs to locate the user\'s Application Support directory, you don\'t need to do a bunch of paranoid conversion of a path constructed with those two components.</p>\n\n<p><em>Edited to add:</em>  I would strongly caution <strong>against</strong> arbitrarily converting everything to UTF-8 using something like <code>wcstombs</code> because filesystem encoding is not necessarily identical to the generated UTF-8.  Mac OS X and Windows both use specific (but different) canonical decomposition rules for the encoding used in filesystem paths.</p>\n\n<p>For example, they need to decide whether "é" will be stored as one or two code units (either <code>LATIN SMALL LETTER E WITH ACUTE</code> or <code>LATIN SMALL LETTER E</code> followed by <code>COMBINING ACUTE ACCENT</code>).  These will result in two different — and different-length — byte sequences, and both Mac OS X and Windows work to avoid putting multiple files with the same name (as the user perceives them) in the same directory.</p>\n\n<p>The rules for how to perform this canonical decomposition can get pretty hairy, so rather than try to implement it yourself it\'s best to leave it to the functions the system frameworks have provided for you to do the heavy lifting.</p>\n'}, {'answer_id': 161798, 'author': 'Mecki', 'author_id': 15809, 'author_profile': 'https://Stackoverflow.com/users/15809', 'pm_score': 2, 'selected': False, 'text': '<p>@JKP:</p>\n\n<p>Not all functions in MacOS X accept UTF8, but filenames and filepaths may be UTF8, thus all POSIX functions dealing with file access (open, fopen, stat, etc.) accept UTF8.</p>\n\n<p>See <a href="http://lists.apple.com/archives/applescript-users/2002/Sep/msg00319.html" rel="nofollow noreferrer">here</a>. Quote:</p>\n\n<blockquote>\n  <p>How a file name looks at the API level\n  depends on the API. Current Carbon\n  APIs handle file names as an array of\n  UTF-16 characters; POSIX ones handle\n  them as an array of UTF-8, which is\n  why UTF-8 works well in Terminal. How\n  it\'s stored on disk depends on the\n  disk format; HFS+ uses UTF-16, but\n  that\'s not important in most cases.</p>\n</blockquote>\n\n<p>Some other POSIX functions handle UTF8 as well. E.g. functions dealing with user names, group names or user passwords use UTF8 to store the information (thus a user name can be Japanese and your password can be Chinese, no problem).</p>\n\n<p>But not all handle UTF8. E.g. for all string functions an UTF8 string is just a normal C String and characters above 126 have no special meaning. They don\'t understand the concept of multiple bytes (chars in C) forming a single Unicode character. How other APIs handle char * pointer being passed to them is different from API to API. However, as a rule as the thumb you can say:</p>\n\n<p>Either the function only accepts C strings with pure ASCII characters (only in the range 0 to 126) or it will accept UTF8. Usually functions don\'t allow characters above 126 and interpret them in any other encoding than UTF8. If this really was the case, it is documented and then there must be a way to pass the encoding along with the string.</p>\n'}, {'answer_id': 265407, 'author': 'Vincent Robert', 'author_id': 268, 'author_profile': 'https://Stackoverflow.com/users/268', 'pm_score': 5, 'selected': True, 'text': '<p>POSIX API in Mac OS X are usable with UTF-8 strings. In order to convert a wchar_t string to UTF-8, it is possible to use the CoreFoundation framework from Mac OS X. </p>\n\n<p>Here is a class that will wrap an UTF-8 generated string from a wchar_t string.</p>\n\n<pre><code>class Utf8\n{\npublic:\n    Utf8(const wchar_t* wsz): m_utf8(NULL)\n    {\n        // OS X uses 32-bit wchar\n        const int bytes = wcslen(wsz) * sizeof(wchar_t);\n        // comp_bLittleEndian is in the lib I use in order to detect PowerPC/Intel\n        CFStringEncoding encoding = comp_bLittleEndian ? kCFStringEncodingUTF32LE\n                                                       : kCFStringEncodingUTF32BE;\n        CFStringRef str = CFStringCreateWithBytesNoCopy(NULL, \n                                                       (const UInt8*)wsz, bytes, \n                                                        encoding, false, \n                                                        kCFAllocatorNull\n                                                        );\n\n        const int bytesUtf8 = CFStringGetMaximumSizeOfFileSystemRepresentation(str);\n        m_utf8 = new char[bytesUtf8];\n        CFStringGetFileSystemRepresentation(str, m_utf8, bytesUtf8);\n        CFRelease(str);\n    }   \n\n    ~Utf8() \n    { \n        if( m_utf8 )\n        {\n            delete[] m_utf8;\n        }\n    }\n\npublic:\n    operator const char*() const { return m_utf8; }\n\nprivate:\n    char* m_utf8;\n};\n</code></pre>\n\n<p>Usage:</p>\n\n<pre><code>const wchar_t wsz = L"Here is some Unicode content: éà€œæ";\nconst Utf8 utf8 = wsz;\nFILE* file = fopen(utf8, "r");\n</code></pre>\n\n<p>This will work for reading or writing files.</p>\n'}, {'answer_id': 20800154, 'author': 'AVG', 'author_id': 1421589, 'author_profile': 'https://Stackoverflow.com/users/1421589', 'pm_score': 0, 'selected': False, 'text': '<p>I have read file name from configuration UTF8 file through <strong>wifstream</strong> (it uses <strong>wchar_t</strong> buffer). </p>\n\n<p>Mac implementation is different from Linux and Windows. \nwifstream reads each byte from file to separate wchar_t cell in the buffer. So we have 3 empty bytes, although <strong>open</strong> requires <strong>char</strong> string. Thus programmer can use <strong>wcstombs</strong> function to convert wide character string to multi-byte string. </p>\n\n<p>The API supports UTF8. For better understanding use memory watcher and hex editor for your file.</p>\n'}]
['https://Stackoverflow.com/questions/12319', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/268/']