PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > Locale/UTF-8 file path with std::ifstream
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Locale/UTF-8 file path with std::ifstream

Réponse
 
LinkBack Outils de la discussion
Vieux 08/02/2008, 09h16   #1
mpalomas@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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 , hint, or suggestion would be appreciated !

Regards,

Michaël

  Réponse avec citation
Vieux 08/02/2008, 12h32   #2
Daniel T.
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Locale/UTF-8 file path with std::ifstream

"mpalomas@gmail.com" <mpalomas@gmail.com> wrote:

> 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 , 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.
  Réponse avec citation
Vieux 08/02/2008, 13h28   #3
mpalomas@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Locale/UTF-8 file path with std::ifstream

On 8 fév, 13:32, "Daniel T." <danie...@earthlink.net> wrote:
> 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 !
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 00h13.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,10381 seconds with 11 queries