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, 3:06 PM   #1
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 599
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, 5: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, 5:53 PM   #3
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 599
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, 6:03 PM   #4
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, 6:04 PM   #5
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, 7:34 PM   #6
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 599
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 7:40 PM.
Ancient Dragon is offline   Reply With Quote
Old Jun 13th, 2005, 6:01 PM   #7
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
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 2:09 PM.

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