473,396 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Locale/UTF-8 file path with std::ifstream

Hi C++ folks,

I have trouble to open files whose path contains non-ascii characters
with std::ifstream.
For instance let's say i just have a file which has Japanese
characters either in the file path or the file name : 疑問.dat. The
file itself does not contains unicode characters or whatever, it is a
binary file, but the file name, or path, contains non-ascii
characters, here it is Japanese but it could be anything else, i know
nothing about my customers languages.

On Linux, simply doing this :
std::ifstream ifs('疑問.dat"); just works, i can open the file.. Of
course in my app file path are not hard coded in the source code, the
user choose his file using a file dialog. The file dialog returns me a
QString (Trolltech Qt framework). If the returned QString is named
filepath, then std::ifstream ifs(filepath.toUtf8()) works fine too.

The problem is my application is cross-platform, and on Windows XP the
2 above pieces of code does not work at all ! The ifstream fails to
open the file ... I suspect this is a locale issue, i just know my
Linux distribution uses UTF-8 by default, this must be why it works,
whereas on Windows, it seems strange, ifstream.getloc().name() returns
just "C" , and if i create a default locale with std::locale
my_locale("") , then my_locale.name() returns French_France.1252 ...

I'm stuck with all this locale/encoding problems, it is not clear in
my mind, to solve a problem firstly you need to understand the
problem, and i think i don't :-) I wonder if i have to change the
locale on Windows to a UTF-8 one (i didn't succeed), if i have to use
some conversion functions, or wifstream, or wstring, even after
searching on Google i didn't made any progress, internationalization
stuff does not seem to be trivial with C++ .

Any help, hint, or suggestion would be appreciated !

Regards,

Michaël

Feb 8 '08 #1
2 15147
"mp******@gmail.com" <mp******@gmail.comwrote:
I have trouble to open files whose path contains non-ascii characters
with std::ifstream.
For instance let's say i just have a file which has Japanese
characters either in the file path or the file name : ^.dat . The
file itself does not contains unicode characters or whatever, it is a
binary file, but the file name, or path, contains non-ascii
characters, here it is Japanese but it could be anything else, i know
nothing about my customers languages.

On Linux, simply doing this :
std::ifstream ifs('^.dat"); just works, i can open the file. Of
course in my app file path are not hard coded in the source code, the
user choose his file using a file dialog. The file dialog returns me a
QString (Trolltech Qt framework). If the returned QString is named
filepath, then std::ifstream ifs(filepath.toUtf8()) works fine too.

The problem is my application is cross-platform, and on Windows XP the
2 above pieces of code does not work at all ! The ifstream fails to
open the file ...
Any help, hint, or suggestion would be appreciated !
Can you convert a QString into UTF-16LE? Try using that instead of converting to UTF-8 and see what happens.
Feb 8 '08 #2
On 8 fv, 13:32, "Daniel T." <danie...@earthlink.netwrote:
Can you convert a QString into UTF-16LE? Try using that instead of converting to UTF-8 and see what happens.
Yes i can and it worked :-)
I did :
const unsigned short* utf16=filepath.utf16();
And it seems fortunately that on Windows MS added an extension to the
ifstream constructor which takes a const unsigned short* , thus :
std::ifstream ifs(utf16) is fine on Windows, and now all my files with
foreign characters in their path can be loaded.

Thanks a lot Daniel !
Feb 8 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: | last post by:
1. how can I have a unicode string? I see wstring but I dont know if it is unicode typedef basic_string<wchar_t> wstring; if not can I use something like: ? typedef basic_string<short>...
2
by: Vlad | last post by:
I have one problem. The company uses files with contents in German language but stored in Central European DOS-encoding (852). The problem is to write a program which converts it to windows-1252...
12
by: Rafał Maj Raf256 | last post by:
Hi, I have an UNICODE text file endcoded in UTF-8. I should store the UNICODE strings in my program for example in std::wstring right? To be able to work on them normally, so that std::wstring...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.