Environment: Gcc/G++ Linux
I have a non-ascii file in file system and I’m going to open it.
Now I have a wchar_t*, but I don’t know how to open it. (my trusted fopen only opens char* file)
Please help. Thanks a lot.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There are two possible answers:
If you want to make sure all Unicode filenames are representable, you can hard-code the assumption that the filesystem uses UTF-8 filenames. This is the “modern” Linux desktop-app approach. Just convert your strings from
wchar_t(UTF-32) to UTF-8 with library functions (iconvwould work well) or your own implementation (but lookup the specs so you don’t get it horribly wrong like Shelwien did), then usefopen.If you want to do things the more standards-oriented way, you should use
wcsrtombsto convert thewchar_tstring to a multibytecharstring in the locale’s encoding (which hopefully is UTF-8 anyway on any modern system) and usefopen. Note that this requires that you previously set the locale withsetlocale(LC_CTYPE, "")orsetlocale(LC_ALL, "").And finally, not exactly an answer but a recommendation:
Storing filenames as
wchar_tstrings is probably a horrible mistake. You should instead store filenames as abstract byte strings, and only convert those towchar_tjust-in-time for displaying them in the user interface (if it’s even necessary for that; many UI toolkits use plain byte strings themselves and do the interpretation as characters for you). This way you eliminate a lot of possible nasty corner cases, and you never encounter a situation where some files are inaccessible due to their names.