473,761 Members | 9,477 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.to Utf8()) 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.1 252 ...

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, internationaliz ation
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 15305
"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.to Utf8()) 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...@earth link.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
818
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> unicode_string; 2. how can I load a (previous) unicode_string from a stream? What stream I must use instead of ifstream? Can I use this: ? typedef basic_ifstream<wchar_t, char_traits<wchar_t> > unicode_ifstream;
2
2952
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 encoding (default encoding on m machine). I wrote a simple code (VC++): ifstream old; locale loc("German_Germany.852"); old.imbue(loc); old.open(oldName);
12
9903
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 foo; foo would mean 5-th _character_, and not 5-th byte of UNICODE encoded string. How do I read a text from UTF-8 file into std::wstring? I need to do some conversion right? from utf-8 to internal format used by
0
9554
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9376
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9923
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8813
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, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5266
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3911
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3509
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2788
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.