Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 13th, 2005, 2:06 PM   #1
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 549
Rep Power: 4 Ancient Dragon is on a distinguished road
UNICODE and ifstreams

my VC++ 6.0 MFC program is compiled for UNICODE. I have a std::wstring object that contains a filename. I tried to use wifstream in(filename.c_str()) but got error that the compiler can't convert the first parameter from wchar_t* to char*. Is there another way to do it without explicitly converting it myself? If not, how do people using other languages use anything from the fstream STL library, or is the C-style FILE the only way?

Thanks
Ancient Dragon is offline   Reply With Quote
Old Jun 13th, 2005, 4:50 PM   #2
Scorpions4ever
Programmer
 
Join Date: Jun 2005
Posts: 86
Rep Power: 4 Scorpions4ever is on a distinguished road
Darn site keeps going up and down. Anyway, one way to do this is to convert the filename to type string like this:
#include <string>
#include <fstream>
#include <iostream>
using namespace std;

int main(void) {
  wstring filename = L"string.cpp";
  string s_filename(filename.begin(), filename.end());
  wifstream in(s_filename.c_str());
  wstring line;
  while (!in.eof()) {
    getline(in, line);
    wcout << line << endl;
  }
  in.close();
  return 0;
}
Scorpions4ever is offline   Reply With Quote
Old Jun 13th, 2005, 4:53 PM   #3
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 549
Rep Power: 4 Ancient Dragon is on a distinguished road
I don't want to convert to ascii -- what if the filename is Chinese or Japanese, or some other language that doesn't fit in ascii???
Ancient Dragon is offline   Reply With Quote
Old Jun 13th, 2005, 5:01 PM   #4
uman
Expert Programmer
 
Join Date: Dec 2004
Posts: 794
Rep Power: 4 uman is on a distinguished road
Does WIndows even allow wide-character filenames?
uman is offline   Reply With Quote
Old Jun 13th, 2005, 5:03 PM   #5
Scorpions4ever
Programmer
 
Join Date: Jun 2005
Posts: 86
Rep Power: 4 Scorpions4ever is on a distinguished road
BTW, I forgot to mention that the solution above is not necessarily portable either. Also, if the filename is something in japanese or whatever, wifstream fails. Several people have proposed fixes to the STL library for this. Apparently, one of the things holding this up is that several filesystems don't actually support unicode.
Scorpions4ever is offline   Reply With Quote
Old Jun 13th, 2005, 5:04 PM   #6
Scorpions4ever
Programmer
 
Join Date: Jun 2005
Posts: 86
Rep Power: 4 Scorpions4ever is on a distinguished road
BTW dinkumware (the guys that Microsoft uses for their STL implementation) apparently has a lib that supports wifstream with a wchar_t * filename.
Scorpions4ever is offline   Reply With Quote
Old Jun 13th, 2005, 6:34 PM   #7
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 549
Rep Power: 4 Ancient Dragon is on a distinguished road
well, thanks for the comments. Not what I wanted to hear, so I guess I'll have to use wfopen() and associated functions.

Not supporting UNICODE is a lousy excuse for not fixing wifstream() -- there are a lot of other STL unicode classes too, such as wstring that don't work on os without unicode support.

Quote:
Originally Posted by uman
Does WIndows even allow wide-character filenames?
Windows is not the only os that support UNICODE -- *nix and others do too.

Last edited by Ancient Dragon; Jun 13th, 2005 at 6:40 PM.
Ancient Dragon is offline   Reply With Quote
Old Jun 16th, 2005, 3:20 PM   #8
obirsoy
Newbie
 
Join Date: May 2005
Posts: 5
Rep Power: 0 obirsoy is on a distinguished road
Quote:
Originally Posted by Ancient Dragon
Not supporting UNICODE is a lousy excuse for not fixing wifstream() -- there are a lot of other STL unicode classes too, such as wstring that don't work on os without unicode support.

Windows is not the only os that support UNICODE -- *nix and others do too.
It is not that easy. The below quote is taken from the FAQ of filesystem library in boost. AFAIK POSIX systems do not support wide character file names. They do support UTF-8.

Quote:
Why aren't wide-character names supported? Why not std::wstring or even a templated type?

Wide-character names would provide an illusion of portability where portability does not in fact exist. Behavior would be completely different on operating systems (Windows, for example) that support wide-character names, than on systems which don't (POSIX). Providing functionality that appears to provide portability but in fact delivers only implementation-defined behavior is highly undesirable. Programs would not even be portable between library implementations on the same operating system, let alone portable to different operating systems.

The C++ standards committee Library Working Group discussed this in some detail both on the committee's library reflector and at the Spring, 2002, meeting, and feels that (1) names based on types other than char are extremely non-portable, (2) there are no agreed upon semantics for conversion between wide-character and narrow-character names for file systems which do not support wide-character name, and (3) even the committee members most interested in wide-character names are unsure that they are a good idea in the context of a portable library.

[October, 2002 - PJ Plauger has suggested a locale based conversion scheme. Others have indicated support for such an experiment.]
PJ Plauger is the president(?) of Dinkunware and as far as I can understand that portion of his library is just an experiment to understand the real issues involved in both implementing and using wide character file names.
obirsoy is offline   Reply With Quote
Old Jun 16th, 2005, 3:52 PM   #9
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 549
Rep Power: 4 Ancient Dragon is on a distinguished road
That makes no sense -- Windows (and possibly others) have wfopen() which takes a wchar_t* for filename. So why can't fstreams? And from what little I could find about it with google, there is apparently a C ANSI standard for it.
Ancient Dragon is offline   Reply With Quote
Old Jun 16th, 2005, 4:34 PM   #10
obirsoy
Newbie
 
Join Date: May 2005
Posts: 5
Rep Power: 0 obirsoy is on a distinguished road
Looks like today is my quote day :p

This is taken from POSIX standard (IEEE Std 1003.1, 2004 Edition )

Quote:
4.6 Filenames
For a filename to be portable across implementations conforming to IEEE Std 1003.1-2001, it shall consist only of the portable filename character set as defined in Portable Filename Character Set. The hyphen character shall not be used as the first character of a portable filename. Uppercase and lowercase letters shall retain their unique identities between conforming implementations. In the case of a portable pathname, the slash character may also be used.
Quote:
3.276 Portable Filename Character Set
The set of characters from which portable filenames are constructed. A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9 . _ - The last three characters are the period, underscore, and hyphen characters, respectively.
As I see it, having wfopen is just a convience (misleading one I should add)for doing the conversions yourself. Basically do not expect your code to work on different locales, just because you used wfopen.

Are you sure wfopen is a standard function? For example the POSIX standard mentioned above does not contain such a function.
obirsoy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:06 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC